Questions tagged [use-state]

useState related questions for reactjs hooks api.

Basic React Hook

useState

const [state, setState] = useState(initialState);

Returns a state value, and a function to update it.

Resources

useState hook reference

Common Questions/Issues

  • Why isn't React state update immediate?
  • How to access the latest value?

useState set method not reflecting change immediately

3329 questions
0
votes
0 answers

setCount not triggering rerender when called from interval

I am trying to update some data on my website roughly once per second. Since I have been getting some strange behaviour, I added a counter to see what was happening. I have used google and tried suggestions, but most seem to reference setState, not…
KKie
  • 21
  • 2
0
votes
2 answers

React useState() & useEffect: DOM element changed but not rendering

I'm trying to achieve in react: click the heart to like/unlike a move, using useState() & useEffect(), but seems the element is applied with new class name, but the color didn't change when i clicked. export function MovieCard2(props) { const…
Hai Na Zheng
  • 167
  • 3
  • 11
0
votes
0 answers

Why is my set data with useState not updating the state?

setAcessKeys is not updating the state immediately even though the data is available at that point which I know through the console.log(data) or by passing the array manually. I realized that without useEffect, it renders multiple times and the…
0
votes
1 answer

react-native : how to sort data before screen is drawn?

I call data by using useQuery and gql. const SEE_ALL_FEED_ORDER = gql` query seeAllFeedOrder { seeAllFeedOrder { id name avatar directFeedNumber } } `; const { data: allFeedData, loading: allFeedDataLoading,…
Hyejung
  • 902
  • 1
  • 8
  • 22
0
votes
1 answer

Conditional rendering with useEffect / useState, why is there a delay in the DOM when switching components?

Intro / Context I am trying to build an application using React that allows for image or video display based on a chosen menu item. I am currently using Advanced Custom Fields within WordPress to build my data objects and using graphQL to query them…
Kevin
  • 137
  • 1
  • 12
0
votes
1 answer

state issue when clicking between different divs

I am having an issue with my application's state tracking. I have an edit form, and when I click on a ticket to set it as the selected ticket, I pass the selected ticket down to the edit form. However, I think I have an issue with the way my state…
Brad Bieselin
  • 107
  • 1
  • 11
0
votes
1 answer

What should i do to change select options after changeing previous input without one step gap? react

I have an input with label "Ilosc osob". When I change it, I want to change the Select's options, depends of number in input. It happens, but with one step gap. What should I do? matchedTables depends on props.tables, and it is filtered array from…
cguy
  • 1
  • 1
0
votes
1 answer

How to use state variable for conditional declaring of array of objects?

In React js, What I want to do is: declare an array of objects (routes) and export. For conditional declaration of array of objects, I want to use an state variable (which returns whether user logged in or not). Aim: user logged in, show Put Rating…
0
votes
2 answers

Why is my variable emptied in React.js when I use setState()

I am creating a simple list of cards and I need to filter it according to which tags are selected. I have: function Homepage() { const [cards, setCards] = useState([]); const [filteredCards, setFilteredCards] = useState([]); let filteredTags =…
Martina
  • 3
  • 1
0
votes
1 answer

Why is data from useQuery undefined when I start screen?

When I start screen, I get data from useQuery And by using useEffect, I get data and immediately sort this data. After sorting data, I put this data to useState named flatlistdata. And I draw screen by using Flatlist with this data. Flatlist data…
Hyejung
  • 902
  • 1
  • 8
  • 22
0
votes
1 answer

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop with my useState in nextjs

I need to change the state and use it but isn't possible and i don't now whhy !!!! const [valid, setValid] = useState(false) let emailsApplied = [] candidatures.map(c => { emailsApplied.push(c.email) }) let…
Camille
  • 57
  • 4
0
votes
3 answers

Use same useState in different components

I have a main app and two components. It looks something like this: MainApp: const MainApp = () => { return ( <> ); }; Component1: const Component1 = () => { …
Denver Dang
  • 2,433
  • 3
  • 38
  • 68
0
votes
1 answer

Is it costly to have too many redux stores?

Currently, each of my components' state is stored in useState hooks. I want to be able to persist the states of all my components so that when a user navigates back to a previous page, etc., they will pick up where they left off. Is this too costly?
0
votes
0 answers

Why does useEffect return the first value and then last value when called instead of each render?

I have a group of functions that are responsible for adding and removing sections from my form in my app. I use a counter (subjectAddressCounter) in my state that keeps track of the iteration of the section being added. This works as expected when…
London804
  • 1,072
  • 1
  • 22
  • 47
0
votes
0 answers

A component is changing a controlled input to be uncontrolled

I made a more basic version of wordle which gives me a warning every time I type in the text box const Wordleinput = (props) => { var targetWordString = "DOGS" const handleUserInput = (e) => { …