Skip to content

Zignal-React/zignal-persist

Repository files navigation

@zignal/persist

npm version MIT License bundle size npm downloads

Persistence plugin for @zignal/core signal stores. Adds localStorage/sessionStorage support to your state.

Install

npm install @zignal/core
npm install @zignal/persist
# or
yarn add @zignal/core
yarn add @zignal/persist
# or
pnpm add @zignal/core
pnpm add @zignal/persist

You must also install @zignal/core.

Usage

import { createZignal } from '@zignal/core';
import { write } from '@zignal/persist';

const useCounter = createZignal(0); // non-persistent
const usePersistentCounter = write(createZignal(0), { key: 'counter', storage: 'localStorage' });

function Counter() {
  const [count, setCount] = useCounter();
  // ...
}

function PersistentCounter() {
  const [count, setCount] = usePersistentCounter();
  // ...
}

API

write(hook, options)

Wraps a signal store hook to persist its value to localStorage or sessionStorage.

  • hook: The hook returned by createZignal.
  • options.key: Storage key (string, optional).
  • options.storage: 'localStorage' (default) or 'sessionStorage'.

cleanupZignal(key?)

Removes all (or one) persisted zignal keys from storage.


MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published