question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Batching update in react-hooks

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Reactions:39
  • Comments:41 (2 by maintainers)

github_iconTop GitHub Comments

59reactions
markeriksoncommented, Nov 17, 2018

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.

41reactions
tqwewecommented, Mar 19, 2019

Spent some time at work today trying to find a good solution to this. Ended up merging my two states (isAuthenticating and user) into an object to solve this but I would certainly love to see some kind of function such as useBatch(() => {}) become available to use.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found