Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
According react-redux docs this is not required with React 18 in use: https://react-redux.js.org/api/batch
But I had some doubts as explained below:
As of React 18, React has improved its batching mechanism for React state updates. That means the useState and useReducer hooks in React, the updates to those state setters will be batched automatically when they occur inside React event handlers or lifecycle methods.
Redux itself does not use React's state mechanism. It uses its own store to manage the global state. When you dispatch actions in Redux, and if those actions lead to state changes in the Redux store, you will typically want your React components to re-render to reflect those changes. This is where the react-redux library comes in. It connects your React components to the Redux store and ensures re-renders when necessary.
Before React 18, if multiple Redux actions were dispatched outside of a React event handler (e.g., in async functions), you might experience multiple re-renders in your React components, as react-redux wouldn't batch these updates by default. To mitigate this, react-redux provided a batch function to group together multiple Redux updates, ensuring a single re-render for all of them.
With React 18's enhanced batching mechanism, the React updates triggered by Redux state changes might be batched more effectively, even if they occur outside React event handlers or lifecycle methods. That's why the recommendation might be there.
However, there's one thing to note: Just because React 18 batches its state updates more effectively doesn't mean it will batch all updates from any possible source. The extent to which React 18's batching applies to react-redux updates would depend on the integration details of the react-redux library with React.
Stack Overflow link saying the opposite: https://stackoverflow.com/questions/76313875/react-redux-batch-on-react-v-18
So in any case even in case of React 17 would it not make sense to leverage batch?
Also one consideration: While the new version 9 uses useLayoutEffect to ensure the effect has fired before the next repaint it does not mean that batching is not required as still the store is getting updated.
All of this might be totally wrong, so looking forward to your feedback on this.
I had tested this on my local could not clearly make out an improvement.
For my use case I am using RTK which has a autoBatchEnhancer so not sure if this is would not be true even in React 17.