1.2.12 • Published 10 months ago

zustand-persist-cookie-storage v1.2.12

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

zustand-persist-cookie-storage

A simple package created to allow persisting a zustand store to a cookie 🙂. The package will automatically apply an expiry for the cookie as far in the future as possible (you can override this if you want).

Install

pnpm add zustand-persist-cookie-storage
pnpm add -D @types/js-cookie

Usage

  1. Import the package using import CookieStorage from 'zustand-persist-cookie-storage'.
  2. Import createJSONStorage from zustand using import { createJSONStorage } from 'zustand/middleware'.
  3. Create your store normally.
  4. In the persist options, set storage to storage: createJSONStorage(() => CookieStorage()).
  5. Pass any cookie options to CookieStorage.

Your code should look something like:

import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import CookieStorage from 'zustand-persist-cookie-storage';

export const usePersistedZustandStore = create(
  persist(
    ...,
    {
      name: ...,
      storage: createJSONStorage(() => CookieStorage({...[COOKIE OPTIONS GO HERE]}))
    }
  )
);

Cookie Options

The type of cookie options should match CookieAttributes from @types/js-cookie (see @types/js-cookie for more info).

1.2.12

10 months ago

1.2.11

10 months ago

1.2.10

10 months ago

1.2.9

10 months ago

1.2.8

10 months ago

1.2.7

11 months ago

1.2.6

11 months ago

1.2.5

11 months ago

1.2.4

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago