Zustand Middleware Execution Timing versus React Automatic Batching: Pre‑process or Post‑process?
23.5K reputation · 29 Mar 2021, 12:46 UTC
Goal: Decide whether Zustand middleware should execute as a pre‑process step (before the store’s internal state is mutated) or as a post‑process step (after state is set but before subscribers are notified) to align with React 18’s automatic batching and avoid unnecessary re‑renders.
Uncertainty: The current implementation wraps setState and runs middleware logic around the state update, but the community has not settled on whether the wrapper should invoke the middleware before or after the mutation. This ordering affects when logging sees the old vs. new state, how persistence captures snapshots, and whether synchronous work adds to the render‑cycle cost under concurrent mode.
Specific questions:
- Should Zustand call middleware before mutating the store so that observers see the previous state?
- Should middleware run after the state is set but before notifying subscribers to allow post‑process side effects?
- How does each ordering behave with React’s automatic batching in concurrent mode, and which yields fewer extraneous commits?