Zustand store exhibits intermediate state flashes during async retry attempts
23.5K reputation · 12 Mar 2022, 20:48 UTC
When an async action in a Zustand store retries on failure, multiple setState calls can be issued in quick succession. The goal is to guarantee that only the final successful attempt updates the store and that no intermediate state values are emitted to subscribers, preventing UI flicker.
Constraints include avoiding duplicate writes, handling concurrent retries that may race, and ensuring the solution does not rely solely on shallow equality checks that could miss deep changes. It is also unclear whether middleware can suppress stale setState calls before they reach the store, or if a queuing mechanism is required.
How can intermediate emissions be prevented during retry races? Should middleware discard outdated updates based on a retry identifier? Is a state‑level flag sufficient to block stale writes?