Batch setState
See original GitHub issueHello!
I’m reviewing this store library, testing out some quirks and stuff. It seems really great, but I would suggest adding some type of batch update when updating multiple stores, if it doesn’t already exist.
// actions
export const action = () => {
modalStoreApi.setState({ show: false });
usersStoreApi.setState({ users: [ ...usersStoreApi.getState().users, 'new user' ] })
}
// component
export const Component = () => {
const show = useModalStore(store => store.show)
const users = useUsersStore(store => store.users)
return show && <span>User 1: {users[0]}</span>
})
So in this case, the component will re-render twice, so what would be cool would be something like this, similar to how redux-thunk does it:
// actions
export const action = () => {
batch(() => {
modalStoreApi.setState({ show: false });
usersStoreApi.setState({ users: [ ...usersStoreApi.getState().users, 'new user' ] })
})
}
Not the greatest example perhaps, but I know this is needed at times, when you’re separating your stores, and combining them at places using selectors and so on.
What do you think?
Thanks
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (4 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 >React state batch updating - reduce re-renders | The Startup
“React may batch multiple setState() calls into a single update for performance”, according to React's documentation. Batch updating is a ...
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 not batching your setState updates? Here's what you ...
Until React 18, we only batched updates during the React event handlers. Updates inside of promises, setTimeout, native event handlers, or any ...
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
Don’t trust your console.log to check rerender. Your component is a function, and functions are always execute, it do not means that React actually triggered an update for your component.
You can check that a render occur with a
Math.random()
:I wouldn’t bother, only legacy react doesn’t batch, blocking and concurrent will batch automatically, see: https://reactjs.org/docs/concurrent-mode-adoption.html#feature-comparison
Redux will most likely remove that functionality now that it’s officially in react.
Just do this:
If you don’t want to leave legacy mode, use a small middleware. The following will enable legacy batched updates for zustand, all sets and actions: