createConStore<S extends DS,AR extends ActRecord,SP extends Record<string, unknown>,Sel extends Selector<S, AR, SP> = DefaultSelector<S, AR, SP>,>(initial: Initial<S>,options?: CreateConStoreOptions<S, AR, SP>,selector?: Sel,): CreateConStoreReturnType<S, AR, SP, Sel>
Creates a global state store with history tracking and subscription through React's useSyncExternalStore capabilities. Similar to useCon but for managing global state that persists across component unmounts.
Basic usage
Basic usage
// Create a store const useStore = createConStore({ user: { name: '', age: 0 }, settings: { theme: 'light' } }); // Use in components function UserProfile() { const [state, { set }] = useStore(); return ( <input value={state.user.name} onChange={e => set('user.name', e.target.value)} /> ); }
initial: Initial<S>
- The initial state object or
function
that returns the initial state object
optional
selector: Sel = DefaultSelector
- A function to customize the shape of the returned state.
By default, returns
[state, controls]
. Create your own selector to return a different structure. Receives all controls and state history as props.
createConStore<>(initial: Initial<S>,selector: Sel,): CreateConStoreReturnType<S, Record<never, never>, SP, Sel>
Creates a global state store with history tracking and subscription through React's useSyncExternalStore capabilities. Similar to useCon but for managing global state that persists across component unmounts.
Basic usage
Basic usage
// Create a store const useStore = createConStore({ user: { name: '', age: 0 }, settings: { theme: 'light' } }); // Use in components function UserProfile() { const [state, { set }] = useStore(); return ( <input value={state.user.name} onChange={e => set('user.name', e.target.value)} /> ); }
selector
example
selector
example
// Default selector usage: [state, controls] const useStore = createConStore({ count: 0 }); // Use in component function Component() { const [state, controls] = useStore(); } // Custom selector usage const useStoreWithSelector = createConStore( { count: 0 }, // Custom selector ({ state, setWrap }) => ({ count: state.count, increment: setWrap(draft => { draft.count++ }) }) ); // Use in component function Component() { const {count, increment} = useStoreWithSelector(); } // Overwrite selector function Component() { const {count, increment} = useStoreWithSelector(({ state, setWrap }) => ({ count: state.count, increment: setWrap(draft => { draft.count++ }) })); }
initial: Initial<S>
- The initial state object or
function
that returns the initial state object
optional
selector: Sel = DefaultSelector
- Selector A function to customize the shape of the returned state.
By default, returns
[state, controls]
. Create your own selector to return a different structure. Receives all controls and state history as props.