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

Zustand · Const

reset state

Add a reset action that applies the store’s captured initial state. For multiple stores, collect reset callbacks and invoke them together.

How it works

Define a reset action inside the store creator and call set(store.getInitialState()). Zustand supplies the store API as the third creator argument, allowing the action to retrieve and apply the store’s initial state.

Syntax

reset: () => set(store.getInitialState())

Parameters

NameTypeDescription
storeStoreApi<State>Zustand state creator callback; its third argument provides the store API used to call getInitialState().

Returns

A reset action that updates the store to its initial state; the guide does not specify a separate return value.

Key points

  • A reset action can restore a store to the state it had when it was initialized.
  • The state creator receives store as its third argument, alongside set and get.
  • Call getInitialState() on the store API and pass the result to set to perform the reset.
  • A shared set is suitable for collecting reset functions when an application has multiple stores.

Common mistakes

  • For multiple stores, reset functions can be collected in a shared set and invoked together; the documented helper uses replacement mode so the complete initial state is restored.
  • When resetting through the store API, passing true replaces the entire state rather than shallow-merging it.

Example: Reset a Zustand store

import { create } from 'zustand'

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

const useCounter = create<State>()((set, get, store) => ({
  count: 0,
  reset: () => set(store.getInitialState()),
}))

useCounter.setState({ count: 3 })
useCounter.getState().reset()
console.log(useCounter.getState().count) // 0

The action restores the store to the initial state captured by Zustand.

When to use it

Session cleanup

Clear application state after a user signs out.

Discard in-progress input

Return a form or wizard store to its starting values.

Test isolation

Restore every store between test cases.

Interview questions

QHow do you reset a Zustand store to its initial state?

Add a reset action to the store and implement it with set(store.getInitialState()). The store API is available as the third argument to the state creator.

Follow-up: How would you reset several stores? Create a shared collection of reset callbacks, register one for each store, and invoke every callback when a global reset is needed.

QWhere does the reset action get the initial state?

Use store.getInitialState() to obtain the state captured when the store was created, then apply it with set. This keeps the reset target tied to the store’s own initial value.

Follow-up: Why might replacement mode be preferable for a global reset? It removes any state keys added later instead of retaining them through a shallow merge.

QHow can multiple Zustand stores be reset together?

For a group of stores, register callbacks in a shared Set. Each callback can call store.setState(store.getInitialState(), true), and a coordinator can execute the set.

Follow-up: How can this pattern support tests? Run the collected callbacks after each test so stores return to their initial state.

  • create
  • set
  • get
  • getInitialState
  • setState
  • StateCreator

Source: How to reset state - Zustand Docs. Checked 2026-10-15.