DEV Community

Discussion on: Build a Timer with React Hooks

Collapse
 
atellmer profile image
AlexPlex

Set state in React will called asynchronously one time, so that this optimization doesn't need at all.

Collapse
 
viclafouch profile image
Victor de la Fouchardière

Just console.log, you will see 3 log, because of 3 updates state ;)

Here, we will have only 1 rerender

Thread Thread
 
atellmer profile image
AlexPlex

I checked useState behavoiur and I have 1 render. See example codesandbox.io/s/sweet-star-er91q?...

Thread Thread
 
viclafouch profile image
Victor de la Fouchardière • Edited

It's because it comes from an event, like a click on a button.

React currently will batch state updates if they're triggered from within a React-based event, like a button click or input change. It will not batch updates if they're triggered outside of a React event handler, like an async call.

Thread Thread
 
atellmer profile image
AlexPlex

Yea, you're right. I didn't know it. Thanks. But what's reason for work batching only in event handlers?