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.

Hello!

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:closed
  • Created 4 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
rdhoxcommented, Feb 25, 2020

or at least the components ran my console.log statement twice

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():

return(
  {...some JSX}
  {Math.random()}
);
1reaction
drcmdacommented, Feb 18, 2020

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

*: Legacy mode has automatic batching in React-managed events but it’s limited to one browser task. Non-React events must opt-in using unstable_batchedUpdates. In Blocking Mode and Concurrent Mode, all setStates are batched by default.

Redux will most likely remove that functionality now that it’s officially in react.

Just do this:

ReactDOM.createBlockingRoot(rootNode).render(<App />)

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:

import create from 'zustand'
import { unstable_batchedUpdates } from 'react-dom'

const batched = config => (set, get, api) =>
  config(args => unstable_batchedUpdates(() => set(args)), get, api)

const [useStore] = create(batched(set => ({ ... })))
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 >
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

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 Reddit Thread

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