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

Zustand · Module

getStorage

getStorage is the factory callback passed to createJSONStorage. It returns a Zustand-compatible storage engine so persist can save JSON-encoded state in browser storage or a custom backend.

How it works

getStorage is the callback accepted by createJSONStorage. It supplies the underlying storage engine, while the helper adapts that engine to Zustand's persistence format by using its getItem, setItem, and removeItem operations. The callback form lets storage be selected lazily, which is useful for browser-only globals or alternative storage implementations.

Syntax

createJSONStorage(getStorage: () => StateStorage, options?: JsonStorageOptions)

Parameters

NameTypeDescription
getStorage() => StateStorageA function that returns the storage engine used by the JSON storage adapter.
optionsJsonStorageOptionsOptional JSON storage configuration.

Returns

StateStorage according to the documented signature; the helper description specifies that it returns PersistStorage.

Key points

  • Its first parameter is a zero-argument function returning a StateStorage; optional JSON-storage options may also be supplied.
  • The storage callback uses Zustand's StateStorage type, which can be imported from zustand/middleware.
  • The callback's returned engine needs the three persistence operations used by the adapter.

Common mistakes

  • The callback must provide a storage engine compatible with the expected storage operations; it is intended for custom or alternative storage implementations.

Example: Use sessionStorage with persist

import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'

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

const useStore = create<State>()(
  persist(
    (set) => ({
      count: 0,
      increment: () => set((state) => ({ count: state.count + 1 })),
    }),
    {
      name: 'counter',
      storage: createJSONStorage(() => sessionStorage),
    },
  ),
)

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

Creates a persist-compatible storage adapter around sessionStorage, allowing Zustand to serialize and save state there.

When to use it

Select localStorage or sessionStorage lazily.

Browser persistence

Adapt a storage backend that implements Zustand's storage operations.

Custom persistence

Defer access to browser-only storage until the adapter needs it.

Environment-aware apps

Interview questions

QWhat is the purpose of the getStorage argument?

getStorage is a function that returns a StateStorage instance. createJSONStorage uses it to build a PersistStorage adapter for Zustand's persist middleware.

Follow-up: Why is it a callback instead of passing the storage object directly? It allows storage access to be deferred and makes browser-specific or custom storage selection possible.

QWhat does the storage returned by getStorage need to implement?

The returned storage engine must expose getItem, setItem, and removeItem. These operations are used by the JSON storage adapter to read, write, and delete persisted values.

Follow-up: What must a custom storage implementation provide? It must implement the StateStorage interface expected by the adapter, including those three operations.

QHow is getStorage used with browser storage?

A common call is createJSONStorage(() => sessionStorage). The resulting adapter can be supplied as the storage option in persist, allowing persisted Zustand state to use session storage instead of the default storage.

Follow-up: How would you adapt another backend? Return an object implementing the same storage methods, then pass its factory callback to createJSONStorage.

  • createJSONStorage
  • StateStorage
  • persist
  • JsonStorageOptions

Source: Persisting store data. Checked 2026-10-15.