Scoped stores
Edit independent drafts, re-render their parent, and reset an editor with a new provider key.
Independent editors
Edits stay in their scope. Refreshing the parent preserves drafts; reset starts a new session. Unmounting and mounting the right editor creates a fresh draft.
Left editor
This nested editor has its own provider. Resetting the left scope also remounts its nested scope.
Nested editor
Right editor
Context chooses the instance and useClassyStore subscribes to its values. Changing the provider key starts a fresh draft.
Editor provider
Each editor owns the store created by its provider. This is the provider used by the live example.
import {useLocalStore} from '@codebelt/classy-store/react';
import {prepareStoreForHydration} from '@codebelt/classy-store/react/server';
import {persist, type StorageAdapter} from '@codebelt/classy-store/utils';
import {type ReactNode, useEffect} from 'react';
import {type EditorData, EditorStore} from '../../editorStore';
import {EditorStoreContext} from '../../editorStoreContext';
type Props = {
initialData: EditorData;
children: ReactNode;
persistence?: {name: string; storage: StorageAdapter};
};
/** Own one store per mount. Change the provider's key to start a new session. */
export function EditorStoreProvider({
initialData,
children,
persistence,
}: Props) {
const store = prepareStoreForHydration(
useLocalStore(() => new EditorStore(initialData)),
);
const name = persistence?.name;
const storage = persistence?.storage;
useEffect(() => {
if (name === undefined || storage === undefined) return;
const handle = persist(store, {
name,
storage,
properties: ['title', 'body'],
skipHydration: true,
});
void handle
.rehydrate()
.catch((error) => console.error('Could not restore editor data', error));
return () => handle.dispose();
}, [store, name, storage]);
return (
<EditorStoreContext.Provider value={store}>
{children}
</EditorStoreContext.Provider>
);
}
