Create a scoped Zustand store
A store has an owner
Zustand is a third-party state library. A store holds a snapshot of client-owned data and actions that change it. Components subscribe to the parts they need. Unlike local state, a single store can serve distant components without passing each value through every intermediate parent. Start with local state unless there is a real sharing requirement. All examples use the Zustand 5.x teaching lane, React 18 or 19 and an approved JSX toolchain. Exact package patches and runtime execution remain pending; each code block is a separate App.jsx, not a live ByteMind editor.
import React, {useState, useEffect, createContext, useContext} from 'react';import {create, useStore} from 'zustand';import {createStore} from 'zustand/vanilla';import {useShallow} from 'zustand/react/shallow';import {persist, createJSONStorage, subscribeWithSelector, devtools} from 'zustand/middleware';const useDisplay=create(set=>({density:'comfortable',toggle:()=>set(s=>({density:s.density==='compact'?'comfortable':'compact'}))}));function Preview(){const density=useDisplay(s=>s.density);return <p>Layout: {density}</p>;}function Toolbar(){const toggle=useDisplay(s=>s.toggle);return <button onClick={toggle}>Toggle density</button>;}export default function App(){return <section><Preview/><Toolbar/></section>;}Initially the preview says Layout: comfortable. Pressing Toggle density switches to compact. create produces a bound Hook with store methods attached; call it outside component rendering for a deliberately shared client-only singleton. The selector receives a snapshot. set normally merges returned top-level fields, not deeply nested objects. Functional updates read current store state. get reads the current snapshot inside an action; do not call it before initialization finishes. Avoid treating this singleton as a server request store.
Scope the store to a workspace
import React, {useState, useEffect, createContext, useContext} from 'react';import {create, useStore} from 'zustand';import {createStore} from 'zustand/vanilla';import {useShallow} from 'zustand/react/shallow';import {persist, createJSONStorage, subscribeWithSelector, devtools} from 'zustand/middleware';function createPreferences(initial='comfortable') { return createStore((set,get)=>({density:initial, count:0, toggle:()=>set(s=>({density:s.density==='compact'?'comfortable':'compact'})), increment:()=>set(s=>({count:s.count+1})), reset:()=>set({density:initial,count:0}), isCompact:()=>get().density==='compact'}));}const StoreContext=createContext(null);function Scope({children,initial}){ const [store]=useState(()=>createPreferences(initial)); return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;}function usePrefs(selector){const store=useContext(StoreContext);if(!store)throw new Error('Missing Scope');return useStore(store,selector);}function Panel({label}){const density=usePrefs(s=>s.density);const count=usePrefs(s=>s.count);const toggle=usePrefs(s=>s.toggle);const increment=usePrefs(s=>s.increment);const reset=usePrefs(s=>s.reset);return <section><h2>{label}</h2><p>{density}: {count}</p><button onClick={toggle}>Toggle density</button><button onClick={increment}>Add note</button><button onClick={reset}>Reset</button></section>;}export default function App(){return <><Scope initial="comfortable"><Panel label="Reading"/></Scope><Scope initial="compact"><Panel label="Editing"/></Scope></>;}The reading workspace begins at comfortable: 0 and editing at compact: 0. Adding a note on the left changes only its count. Reset restores the initial density and count without deleting action functions. createStore returns a standalone store; useStore connects it to React. Context carries the store instance, not a changing state object. The lazy initializer constructs it once for the retained scope; development checks may invoke initializers more than once, so creation must not perform external writes. Changing the initial prop does not reset a mounted scope; use an explicit reset or a deliberate keyed remount. For SSR, initialize a separate store per request and supply equivalent initial client data for hydration. Never keep per-user data in a server module singleton.
Compose slices without creating two unrelated stores
import React, {useState, useEffect, createContext, useContext} from 'react';import {create, useStore} from 'zustand';import {createStore} from 'zustand/vanilla';import {useShallow} from 'zustand/react/shallow';import {persist, createJSONStorage, subscribeWithSelector, devtools} from 'zustand/middleware';const layoutSlice=set=>({density:'comfortable',toggle:()=>set(s=>({density:s.density==='compact'?'comfortable':'compact'}))});const noteSlice=set=>({notes:[],add:title=>set(s=>({notes:[...s.notes,title]}))});function makeStore(){return createStore((set)=>({...layoutSlice(set),...noteSlice(set)}));}export default function App(){const [store]=useState(makeStore);const density=useStore(store,s=>s.density);const notes=useStore(store,s=>s.notes);return <section><p>{density}: {notes.length} notes</p><button onClick={()=>store.getState().toggle()}>Toggle</button><button onClick={()=>store.getState().add('Read chapter')}>Add note</button></section>;}Slices organize one state object by responsibility. They share the same setter; middleware belongs around the combined creator. Adding a note copies the array, leaving the old snapshot intact. Avoid name collisions when spreading slices. Calling getState() in the event handler is fine; reading it alone during render does not subscribe. Never replace the whole state with a partial object using the replacement flag, because actions can disappear. Prefer explicit actions and use a subscription for displayed values.
Practice
Create two independent settings panels and describe which values should change when one panel is reset. Add a nested settings object and update one field with a nested copy.