Zustand middleware for syncing state to external stores (Gumnut, Firebase, Y.js, etc).
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]);
}
}
)
);- Wrap values with
synced()to mark them for synchronization - Provide
resolvePropto define how to access properties in your external store
Values marked with synced() will sync to your external store. Everything else stays local.