Batching update in react-hooks
See original GitHub issueDo you want to request a feature or report a bug?
bug + improvement
What is the current behavior?
Sometimes state updates caused by multiple calls to useState
setter, are not batched.
sandbox1: https://codesandbox.io/s/8yy0nw2m28 sandbox2: https://codesandbox.io/s/1498n44yr3
Step to reproduce: click on increment all
text and check the console
diff:
// sandbox1
const incAll = () => {
console.log("set all counters");
incA();
incB();
};
// sandbox2
const incAll = () => {
setTimeout(() => {
console.log("set all counters");
incA();
incB();
}, 100);
};
console1:
render
set all counters
set counter
set counter
render
console2:
render
set all counters
set counter
render
set counter
render
What is the expected behavior?
Render function should be called once after multiple calls of setState
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react: 16.7.0-alpha.2
Issue Analytics
- State:
- Created 5 years ago
- Reactions:39
- Comments:41 (2 by maintainers)
Top Results From Across the Web
Does React batch state update functions when using hooks?
If the event handler is react-based then it batches the updates. This is true for both setState ...
Read more >Automatic Batching in React 18: What You Should Know
React uses batching to group state updates within event handlers and inbuilt hooks. It prevents components from re-rendering for each state update and ......
Read more >React state batch updating - reduce re-renders | The Startup
React mechanism of state batch updates. Instead of one by one, React does batch updates, reducing the number of hooks and class component...
Read more >React 18 adds automatic batching - Saeloun Blog
Batching is a React feature that combines all the state updates into a single update, causing a single re-render thereby improving the ...
Read more >Batch Updates in React 17 or Earlier Versions
React batch updates for multiple setState() calls inside setTimeout, promises, and native event handlers.
Read more >Top Related Medium Post
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This appears to be normal React behavior. It works the exact same way if you were to call
setState()
in a class component multiple times.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 a
setTimeout()
.I think there’s plans long-term to always batch events, but not sure on the details.
Spent some time at work today trying to find a good solution to this. Ended up merging my two states (
isAuthenticating
anduser
) into an object to solve this but I would certainly love to see some kind of function such asuseBatch(() => {})
become available to use.