I am so confused here with React useEffect Hooks with setTimeout. I have exactly the same code as w3school https://www.w3schools.com/react/showreact.asp?filename=demo2_react_useeffect_settimeout2
but my code is rendering "I've rendered 2 times" instead of "I've rendered 1 times". And I have tried it in many code editors, and mine always shows 2..
And the only way I can get it to render 1 is refactoring to this instead
useEffect(() => {
setTimeout(() => {
setCount(count + 1);
}, 1000);
}, []);
Could someone please explain to me why? Thanks for your help!
I am expecting my code to render the same result as w3school example.
You clicked {count} times