Redux batching
React Redux exports a function called batch()
, which ensures that multiple actions dispatched outside of React only result in a single render update.
This works out of the box for Rematch and it's recommended in scenarios where our effects contain multiple dispatch executions to other models and we just need a single re-render update.
For example, imagine a Rematch effect that sets products to our state and then executes another effect that pushes some analytics to another model of our store:
dispatch.shop.SET_PRODUCTS({ products: data, totalCount }); dispatch.analytics.OTHER();
Since this logic is inside a Rematch effect, it doesn't run inside a React life cycle and will create two re-renders if we don't wrap this logic inside the batch()
method:
import { batch } from "react-redux"; ... batch(() => { dispatch.shop.SET_PRODUCTS({ products: data, totalCount }); dispatch.analytics...