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

Zustand · Const

resetting state

Add a reset action that calls set(store.getInitialState()) to restore a Zustand store's initial state. For multiple stores, register reset callbacks and invoke them together.

How it works

The documented pattern adds a reset action inside the store creator and calls set with store.getInitialState(). This restores the store's original state on demand. For several stores, keep reset callbacks in a shared Set and invoke each callback when a global reset is needed.

Syntax

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

Parameters

NameTypeDescription
storeStore APIThe store's state creator receives set, get, and store; the reset pattern uses the store API's getInitialState method.

Returns

A reset action returns void; invoking it restores the store to its initial state.

Key points

  • A store can expose an action that restores its state to the initial value.
  • Call set with getInitialState() to restore the store's initial snapshot.
  • The page also documents a coordinated approach for resetting several stores.
  • Keep reset callbacks in a Set so they can be collected centrally.
  • A global reset iterates over the registered callbacks and invokes each one.

Common mistakes

  • When resetting several stores, pass true as the replace argument so the complete state is replaced rather than shallowly merged.

Example: Reset a store to its initial state

import { create } from 'zustand'

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

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

useStore.setState({ count: 5 })
useStore.getState().reset()
console.log(useStore.getState().count) // 0

The store exposes a reset action that replaces its current values with the initial snapshot returned by the store API.

When to use it

Clear session state

A user signs out and application state must return to its original values.

Reset test state

A test suite needs a reusable mechanism to return stores to their baseline.

Reset multiple stores

An application has several independent stores that must be cleared together.

Interview questions

QHow does the documented single-store reset pattern work?

Define reset on the store and implement it with set(store.getInitialState()). The store API supplies the initial snapshot, so the action does not need to duplicate the initial values.

Follow-up: How would you reset every store? Register a callback for each store in a shared Set, then iterate through that Set and call each callback.

QHow can multiple stores be reset at once?

Create each store through a wrapper around the real create function. After creation, add a callback that invokes store.setState with the store's initial state and replacement enabled.

Follow-up: Why use a Set? It provides a central collection of reset callbacks that can be iterated whenever the application needs a global reset.

QWhere should reset behavior be defined?

The reset action is placed in the store definition and can be called from application code like any other action. It restores the values captured as the store's initial state.

Follow-up: What should be checked when adding new stores? Ensure they are created through the wrapper; otherwise their reset callback will not be registered.

  • create
  • set
  • getInitialState
  • setState
  • StateCreator

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