Zustand Persist Middleware Migration: Can Rollback Safely After Schema Version Change?
0 reputation · 23 May 2024, 09:07 UTC
Zustand's persist middleware rehydrates the store from persisted JSON and optionally runs a migrate function that converts outdated state shape to the current TypeScript type, using a version option supplied in the persist configuration.
The migration runs only during initial load, not on subsequent state updates, and Zustand does not automatically roll back when the transform fails or returns an incompatible shape, leaving the developer to decide whether to clear storage or fall back to the initial state.
Given that TypeScript store types are declared separately from persisted JSON, runtime validation of old persisted data against the current type is not performed, so silent mismatches can occur; does Zustand's migrate hook guarantee that a failed migration leaves the store in a consistent state? When a migration throws, does the store revert to its initial state or retain partially transformed data? Can an application implement safe rollback by clearing storage on migration failure without breaking React component re‑rendering?