I am trying to figure out when the re-render occurs when updating state in React with useState hook. In the code below, clicking the button triggers the handleClick function which contains a setTimeout. The callback inside setTimeout is executed after 1 second, which updates the state variable count by calling setCount. A console log then prints a message.
The order I would expect the console logs to show up once the button is clicked are:
- 'Count before update', 0
- 'Count post update in setTimeout', 0
- 'Count in render', 1
However the order I see after running this code is:
- 'Count before update', 0
- 'Count in render', 1
- 'Count post update in setTimeout', 0
How is it that "'Count in render', 1" shows up before "'Count post update in setTimeout', 0"? Doesn't setCount result in the scheduling of a re-render that is not immediate? Shouldn't the console log immediately after the setCount function call always execute before the re-render is triggered?
function AppFunctional() {
const [count, setCount] = React.useState(0);
const handleClick = () => {
console.log('Count before update', count);
setTimeout(() => {
setCount(count + 1);
console.log('Count post update in setTimeout', count);
}, 1000);
};
console.log('Count in render', count);
return (
<div className="container">
<h1>Hello Functional Component!</h1>
<p>Press button to see the magic :)</p>
<button onClick={handleClick}>Increment</button>
{!!count && (
<div className="message">You pressed button {count} times</div>
)}
</div>
);
}
ReactDOM.render(<AppFunctional />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>