- Procedure:
const nextValue = counter + limit_counter; -> In here, nextValue = 2
console.log("haha", nextValue);
setCounter(nextValue); -> nextValue =2 for counter
console.log({ counter }); //but when I check, why is counter = 1 ??????
-
Example images:
enter image description here -
There is App root:
enter image description here -
There is MainCounter component:
enter image description here
Could you explain me the reason why?? Thank you ^^
enter image description here
const nextValue = counter + limit_counter; -> In here, nextValue = 2
console.log("haha", nextValue);
setCounter(nextValue); -> nextValue =2 for counter
console.log({ counter }); //but when I check, why is counter = 1 ??????
I think in line 4, counter should equal to ‘2’ or I missed something?
1
React’s setState function is async, which means you have to do something like this :
useEffect(() => {
console.log(counter);
}, [counter ]);
Arnaud Flaesch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.