Registry / storage / zustand-cookie-storage

zustand-cookie-storage

JSON →
library1.1.1jsnpmunverified

zustand-cookie-storage v1.1.1 is a lightweight utility for persisting Zustand store state in cookies. It integrates with Zustand's built-in persist middleware via `createJSONStorage(() => cookieStorage)`. Key differentiator: purely cookie-based persistence (no localStorage/IndexedDB), making it suitable for server-side rendering or scenarios where cookies are mandatory. The library is actively maintained, with TypeScript types included. It offers limited customization of cookie attributes but does not support asynchronous storage adapters. Alternatives include zustand/middleware's default storage and zustand-persist.

npm install zustand-cookie-storage
INSTALL
IMPORT
SIG · ZUSTAND-COOKIE-STO
Z
zustand-cookie-storage
storagejavascriptv1.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cookieStorage
import { cookieStorage } from 'zustand-cookie-storage'
const cookieStorage = require('zustand-cookie-storage')
Package is ESM; CommonJS require may work but is not guaranteed.
cookieStorage (type)
import type { cookieStorage } from 'zustand-cookie-storage'
import { cookieStorage } from 'zustand-cookie-storage'
If only using cookieStorage as a value, the type import is unnecessary.
default import
import cookieStorage from 'zustand-cookie-storage'
import { default } from 'zustand-cookie-storage'
Package exports a named export 'cookieStorage'. Default import is not available.

Demonstrates creating a Zustand store with cookie persistence using the persist middleware.

import { create } from 'zustand'; import { createJSONStorage, persist } from 'zustand/middleware'; import { cookieStorage } from 'zustand-cookie-storage'; const useStore = create( persist( (set) => ({ count: 0, increase: () => set((state) => ({ count: state.count + 1 })), }), { name: 'my-cookie-store', storage: createJSONStorage(() => cookieStorage), } ) ); export default useStore;
Debug
Known issues
gotchacookieStorage does not support asynchronous initialization; it must be used synchronously with createJSONStorage.
fix
Use createJSONStorage(() => cookieStorage) as shown in the example.
affects: <=1.1.1
gotchaCookie size is limited to ~4KB per cookie. Large state may be truncated or cause errors.
fix
Use partialize option to persist only necessary state fields.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: (0 , zustand_cookie_storage__WEBPACK_IMPORTED_MODULE_0__.cookieStorage) is not a function
Importing cookieStorage incorrectly (e.g., default import or destructuring from wrong module).
fix
Use named import: import { cookieStorage } from 'zustand-cookie-storage'
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getItem')
Using cookieStorage directly without createJSONStorage wrapper.
fix
Use createJSONStorage(() => cookieStorage) in the persist configuration.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
41 hits · last 30 days
node
34
OpenAI (training)
2
Resources
zustand-cookie-storage — npm install zustand-cookie-storage · libregistry