Select localStorage or sessionStorage lazily.
Browser persistence
Zustand · Module
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.
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.
createJSONStorage(getStorage: () => StateStorage, options?: JsonStorageOptions)| Name | Type | Description |
|---|---|---|
getStorage | () => StateStorage | A function that returns the storage engine used by the JSON storage adapter. |
options | JsonStorageOptions | Optional JSON storage configuration. |
StateStorage according to the documented signature; the helper description specifies that it returns PersistStorage.
StateStorage; optional JSON-storage options may also be supplied.StateStorage type, which can be imported from zustand/middleware.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.
Browser persistence
Custom persistence
Environment-aware apps
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.
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.
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.
createJSONStorageStateStoragepersistJsonStorageOptions