2.0.0 • Published 1 year ago

shared-zustand v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
1 year ago

sharedzustand

  • :octopus: <1.5kB cross-tab state sharing for zustand
  • 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
npm install shared-zustand

or

yarn add shared-zustand

Usage (as of zustand version 4)

import { createStore } from "zustand";
import { subscribeWithSelector} from "zustand/middleware";
import { share, isSupported } from "shared-zustand";

// Create any zustand store
const useStore = createStore(subscribeWithSelector((set) => ({ count: 1 })));

// progressive enhancement check.
if ("BroadcastChannel" in globalThis /* || isSupported() */) {
    // share the property "count" of the state with other tabs
    share("count", useStore);
}

// ...

// somewhere in an event handler
useStore.setState((count) => ({ count: count + 1 }));

Note that I was not able to figure out what the new type of the store is, so I removed the typing from the parameters to the share function. Feel free to make a PR if you do.

Dealing the the deprecation warning in zustand version 3

In new versions of zustand the old selector API is deprecated. Sadly this API is fundamental for this package, as it allows for syncing acros tabs to only occur when a synced property of the store changes.

Luckily zustand ships with a new middleware to restore the selector functionality.

import { create } from "zustand/vanilla";
import { subscribeWithSelector } from 'zustand/middleware'
import { share, isSupported } from "shared-zustand";

const useStore = create(subscribeWithSelector((set) => ({ count: 1 })));

In the future, it may be reasonable to change the behavior of this package to not sync only some properties, but all properties of a given store. This however would, unfortunately, be fully not backward compatible and force users to restructure their data storage models.

Original usage (as of zustand versions 1-2)

import { create } from "zustand/vanilla";
import { share, isSupported } from "shared-zustand";

// Create any zustand store
const useStore = create((set) => ({ count: 1 }));

// progressive enhancement check.
if ("BroadcastChannel" in globalThis /* || isSupported() */) {
    // share the property "count" of the state with other tabs
    share("count", useStore);
}

API (has never changed)

share("count", useStore, {
    // if set to true this tab trys to immediately recover the shared state from another tab.
    initialize: true,
    /*
        Each shared property is shared over a specific channel with an name that has to be unique.
        By default the name of the property is used. So if you want to share properties from different
        stores with the same name, set this to something unique.
    */
    ref: "shared-store",
});

Limitations

  • Only JSON-serilizable objects can be shared
2.0.0

1 year ago

1.0.4

2 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

4 years ago