Full-state reset
Reset a store to a known complete snapshot during tests or logout flows.
Zustand · Module
Zustand’s setState replaces the entire store when called with true as its second argument. The replacement must be complete because omitted state properties—including actions—are discarded.
Pass true as the second argument to setState when the supplied value is the complete replacement state. Unlike the default shallow merge, replacement discards existing state data, so the new value must include everything the store should retain.
store.setState(state, true)| Name | Type | Description |
|---|---|---|
state | T | ((state: T) => T) | The complete replacement state, or a function receiving the current state and returning the complete replacement state. |
replace | true | Set to true to replace the entire state instead of shallow-merging it. |
void
true as the second argument to replace rather than merge.import { createStore } from 'zustand/vanilla'
type State = { bears: number }
const store = createStore<State>(() => ({ bears: 3 }))
store.setState({ bears: 5 }, true)
console.log(store.getState()) // { bears: 5 }The second argument requests replacement, so the supplied object becomes the entire store state.
Reset a store to a known complete snapshot during tests or logout flows.
Load a serialized snapshot when it contains every state field required by the store.
Intentionally switch between complete state representations rather than updating individual fields.
replace argument change?By default, setState performs a shallow merge. Passing true as the second argument disables that merge and replaces the complete state with the supplied value.
Follow-up: What must the argument contain? It must be a complete state value, because replacement does not preserve omitted properties.
setState(partial, true)?The replacement value must represent the entire state. For a state containing actions, omitting an action removes it, which can make the store unusable.
Follow-up: Why is store.setState({}, true) rejected by stricter TypeScript types? Because an empty object is not a valid complete state.
When the flag is computed dynamically, TypeScript may not select the intended overload safely. The documented workaround casts the argument tuple to Parameters<typeof store.setState> before spreading it.
Follow-up: What should be checked at runtime? Ensure the replacement branch supplies a complete state, not merely a partial update.
setcreateStoreuseBearStore.setStateimmutable state and merging