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

Zustand · Module

set

Zustand’s set function updates store state using a partial object or an updater function. It shallowly merges top-level fields by default, while a replacement flag can replace the entire state.

How it works

Zustand supplies set to the store initializer so actions can update state. Pass a partial object for direct changes or a function that receives the current state; updates are shallowly merged unless replacement is requested.

Syntax

set(partial, replace?)

Parameters

NameTypeDescription
partialobject | (state) => objectA partial state object or updater function that returns the state update.
replacebooleanOptional flag that replaces the entire store state instead of shallowly merging it.

Returns

The set function updates the store and notifies its listeners; the page does not specify a separate return value.

Key points

  • The initializer receives set, and store actions use it to publish state changes.
  • Returning only changed top-level fields is sufficient because Zustand supplies the default shallow merge.
  • An updater function can calculate a new value from the current state, which is suitable for counters and other derived changes.
  • The default merge is shallow rather than recursive, so nested structures require explicit copying.
  • The second argument can request replacement, causing the returned state to replace rather than merge with the current store.

Common mistakes

  • Nested objects are not merged recursively. Preserve unchanged nested properties explicitly, typically by copying each relevant level before changing a value.
  • Passing true as the replacement argument discards the existing store state and uses the returned value, so omitted actions or fields are removed.

Example: Updating store state

import { create } from 'zustand'

type Store = {
  count: number
  increment: () => void
  reset: () => void
}

const useStore = create<Store>((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
  reset: () => set({ count: 0 }),
}))

export function Counter() {
  const count = useStore((state) => state.count)
  return <button onClick={useStore.getState().increment}>{count}</button>
}

The store exposes actions that call set with either a partial object or an updater function.

When to use it

Computed updates

A new value depends on the current value, such as incrementing a counter.

Partial updates

Only selected top-level fields need changing while the remaining store state stays intact.

Full replacement

A complete state object must replace the current store value.

Immutable nested updates

A nested object needs modification without mutating the existing reference.

Interview questions

QWhat does Zustand's set function do?

set updates the store and normally shallowly merges the supplied partial state into the existing state. It can receive either an object or an updater function that derives the update from current state.

Follow-up: How do you update a nested object safely? Create a new object for the changed level and spread the existing nested value so unrelated properties remain intact.

QWhen should set receive a function?

Use an updater function when the next value depends on current state, such as incrementing a counter. This form receives the current state and returns the partial update.

Follow-up: Why avoid mutating the current object? Zustand relies on immutable reference changes to detect updates and notify subscribers correctly.

QHow does Zustand handle nested state updates?

By default, set merges only the top level. For a nested object, explicitly copy the nested object and return the updated copy.

Follow-up: What does set(state => newState, true) do? It replaces the complete store state instead of performing the default merge.

  • setState
  • create
  • get
  • immutable state and merging
  • updating state

Source: Immutable state and merging. Checked 2026-10-15.