JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 84832
  • Score
    100M100P100Q181507F
  • License MIT

Zustand middleware to easily persist and sync Zustand state between tabs and windows

Package Exports

  • persist-and-sync
  • persist-and-sync/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (persist-and-sync) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

PersistAndSync Zustand Store Version Downloads Unit Tests

Zustand middleware to easily persist and sync Zustand state between tabs / windows / iframes (SameOrigin)

Motivation: Recently I got cought up in several issues working with persist miggleware and syncing tabs with zustand. This is a simple light weight middleware to persist and instantly share state between tabs or windows

  • ✅ 🐙 (574 Bytes gZiped) ~ 0.5 kB size cross-tab state sharing + persistance for zustand
  • ✅ Full TypeScript Support
  • ✅ solid reliability in 1 writing and n reading tab-scenarios (with changing writing tab)
  • ✅ Fire and forget approach of always using the latest state. Perfect for single user systems
  • ✅ Share state between multiple browsing contexts

Install

$ pnpm add persist-and-sync
# or
$ npm install persist-and-sync
# or
$ yarn add persist-and-sync

Usage

Simply add the middleware while creating the store and the rest will be taken care.

import { create } from "zustand";
import { persistNSync } from "persist-and-sync";

type MyStore = {
  count: number;
  set: (n: number) => void;
};

const useStore = create<MyStore>(
  persistNSync(
    set => ({
      count: 0,
      set: n => set({ count: n }),
    }),
    { name: "my-channel" },
  ),
);

⚡🎉Boom! Just a couple of lines and your state perfectly syncs between tabs/windows and it is also persisted using localStorage!

License

Licensed as MIT open source.


with 💖 by Mayank Kumar Chaudhari