Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zustand · Module

setState (replace)

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.

Since
5

How it works

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.

Syntax

store.setState(state, true)

Parameters

NameTypeDescription
stateT | ((state: T) => T)The complete replacement state, or a function receiving the current state and returning the complete replacement state.
replacetrueSet to true to replace the entire state instead of shallow-merging it.

Returns

void

Key points

  • Without the second argument, updates merge one level into the current state.
  • Use true as the second argument to replace rather than merge.
  • Prefer the default merge for ordinary property changes.
  • Current TypeScript definitions reject incomplete replacement objects.

Common mistakes

  • With replacement enabled, provide the full state shape; a partial object can produce an invalid store.
  • Replacing an object store can remove action functions as well as data.
  • A runtime boolean can create TypeScript problems; use the documented parameter-annotation workaround when needed.

Example: Replace the complete store state

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.

When to use it

Full-state reset

Reset a store to a known complete snapshot during tests or logout flows.

Snapshot hydration

Load a serialized snapshot when it contains every state field required by the store.

State replacement

Intentionally switch between complete state representations rather than updating individual fields.

Interview questions

QWhat does the 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.

QWhat is the main danger of 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.

QHow can dynamic replacement flags be handled in TypeScript?

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.

  • set
  • createStore
  • useBearStore.setState
  • immutable state and merging

Source: createStore - Zustand Docs. Checked 2026-10-15.