Skip to content

gumnutdev/zustand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zustand middleware for syncing state to external stores (Gumnut, Firebase, Y.js, etc).

Usage

import { create } from 'zustand';
import { gumnutSync, synced } from '@gumnutdev/zustand';

const useStore = create(
  gumnutSync(
    (set) => ({
      // Synced state - collaborative across clients
      document: synced({
        title: '',
        content: '',
      }),
      
      // Local state - stays in browser only
      isEditing: false,
      cursorPosition: 0,
    }),
    {
      doc: myGumnutDoc,
      resolveProp: (root, path) => {
        // Tell the middleware how to access properties in your store
        let obj = root;
        for (let i = 0; i < path.length - 1; i++) {
          obj = obj[path[i]];
        }
        return helperForObject(obj).key(path[path.length - 1]);
      }
    }
  )
);

How It Works

  1. Wrap values with synced() to mark them for synchronization
  2. Provide resolveProp to define how to access properties in your external store

Values marked with synced() will sync to your external store. Everything else stays local.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors