Useeffect Hook Not Updating State
I'm following this example but using a functional component. My field is controlled throught state and it works with no problem. However, none of my functions that update the state
Solution 1:
Attention: using setState inside useEffect maybe create an infinite loop, if you update state inside it and re-render run again and the useEffect change the same state again and loop again.
Replace your setState to this change:
setState(state => ({ ...state, username: username }));
Solution 2:
First of all, you need to add methods called in useEffect
to it dependencies
Write your functions with useCallback
hooks as you use them, and wasn't to create new function after every render.
And you must put everything in dependencies which can be changed during the time to have their new values after the change, es they will be memoized
I updated your code and put it here, please check is it what you want?
https://codesandbox.io/s/zealous-grothendieck-y438z?file=/src/App.js
Post a Comment for "Useeffect Hook Not Updating State"