React) State in useState can't receive value [duplicate]
You can't get the updated state value after invoking the setting function! e.g. This is your code block.
setMemo(() => response.data.data);
console.log(memo)
Here is documentation from React website.
https://reactjs.org/docs/faq-state.html
Why is setState giving me the wrong value? In React, both this.props and this.state represent the rendered values, i.e. what’s currently on the screen.
Calls to setState are asynchronous - don’t rely on this.state to reflect the new value immediately after calling setState. Pass an updater function instead of an object if you need to compute values based on the current state (see below for details).
And you can use steState function like the following:
setMemo(response.data.data) // without reference of previous state
setMemo(prevState => ([...prevState, response.data.data]) // update with merged value with previous state.
And to monitor your state change, you need to use useEffect
hook.
useEffect(() => {
// todo for memo change.
}, memo);