How to wait for state to update without useEffect?
Basically I have custom hook that looks like this:
Then I call it like this:
Of course I can use useEffect but I prefer not to.
1 Reply
The reason why it returns stale data is because it closes over the data value for that render. So, whatever value data was when you called execute that is the value that will be logged. You would have to wait for useFoo to re-render in order to see the updated value. You could just return result from execute if you want to see the fresh value after execute or use a ref and update the ref when you update the state in execute.