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

Zustand · Module

getInitialState

getInitialState is an imperative API attached to Zustand stores created with create. It returns the store's initial state, commonly used to implement reset actions.

How it works

getInitialState is an API attached to a store created with create. It returns the state captured when the store was initialized, so an action can pass that value to set to restore the store's initial contents.

Syntax

store.getInitialState()

Returns

The store's initial state snapshot.

Key points

  • A hook returned by create exposes getInitialState as an attached imperative utility.
  • A reset action can pass the initial snapshot directly to set.
  • Its practical purpose is restoring a store to the state it had at initialization.
  • The store object is supplied to the state creator together with set and get.

Common mistakes

  • Use the initial snapshot with set when implementing a reset action; this restores the state to its initial value.
  • Do not call store APIs synchronously while the initial state is being created; use getInitialState later, such as inside an action.

Example: Reset a Zustand store

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: 3 })
useStore.getState().reset()
console.log(useStore.getState().count) // 0

The action restores the store by passing its original snapshot to set.

When to use it

Reset application state

Restore a store after a user logs out or a workflow is cleared.

Testing and manual reset

Provide a reset button or test cleanup action that returns state to its starting snapshot.

Interview questions

QWhat does getInitialState provide?

getInitialState returns the store's initial state snapshot. For a React store created with create, it is available as an attached API utility alongside setState, getState, and subscribe.

Follow-up: How would you reset a store? Define an action that calls set(store.getInitialState()), or use the store's setState method externally with its initial snapshot.

QHow can getInitialState implement reset?

Inside a state creator, the store argument exposes store APIs. A reset action can call set(store.getInitialState()) to replace the current state with the initial value.

Follow-up: Why put the call inside an action? Store APIs are not safe to call synchronously during initialization, so defer the call until after creation.

QWhere is getInitialState exposed?

The API is attached to the hook returned by create. This means consumers can use the hook for React state and also access imperative APIs such as getInitialState.

Follow-up: What other imperative APIs are attached? The documentation lists setState, getState, getInitialState, and subscribe.

  • create
  • setState
  • getState
  • subscribe
  • How to reset state

Source: create. Checked 2026-10-15.