Breaking schema changes
A persisted field is renamed or removed and old storage must be distinguished from the new schema.
Zustand · Pipe
The persist middleware's version option labels the persisted state format. Change it for breaking storage changes, and use migration logic to convert older data to the current shape.
The persist middleware associates stored data with a numeric version. When the configured version differs from the version saved in storage, Zustand does not use that stored value unless migration logic converts it to the current shape.
persist(stateCreator, { name, version, migrate })| Name | Type | Description |
|---|---|---|
version | number | Numeric identifier for the persisted storage schema; defaults to 0. |
migrate | (persistedState, version) => state | Function receiving persisted state and its version, returning state compliant with the latest version. |
const useStore = create(persist((set) => ({ count: 0 }), { name: 'counter', version: 1 }))Assign a new version when the persisted state shape changes.
A persisted field is renamed or removed and old storage must be distinguished from the new schema.
Several historical storage formats need conversion into one current state shape.
An application needs explicit control over whether saved state matches the code's format.
The version option is a number used to identify the persisted state format. Its default is 0, and changing it lets the application signal a breaking storage change.
Follow-up: How should an application transform old data? Pass a migrate function, which receives the persisted state and its saved version and returns state matching the current version.
Increase the version when a persisted field is renamed or another breaking storage change occurs. Zustand compares the stored version with the configured code version before accepting the stored value.
Follow-up: What happens when they differ and no migration is provided? The stored value is not used.
The migrate callback receives two arguments: the persisted state and its version number. It must return data compliant with the latest version configured in the store.
Follow-up: Why is the version argument useful? It allows one migration function to select the appropriate transformation for different historical formats.
persistmigratePersist APIstorage