Documentation Index
Fetch the complete documentation index at: https://mintlify.com/iDevRanjan/nexus-store/llms.txt
Use this file to discover all available pages before exploring further.
useStore is the React hook that connects a component to a NexusStore store. Call it with a store reference and an optional selector function; it returns the selected slice of state and automatically re-renders the component whenever that slice changes. Under the hood it delegates to React’s useSyncExternalStore, which means subscriptions are safe under React 18’s concurrent rendering — no state tearing, no extra batching logic required.
Signature
Parameters
The store object returned by
createStore (or immer(createStore(...))). This is the live store your component will subscribe to. Pass the module-level store constant — never create the store inside the component.A function that receives the full state object and returns the slice you want to subscribe to. When provided, the component only re-renders when the return value of this function changes (compared with
Object.is). When omitted, useStore returns the full state object and re-renders on any change to the store.Returns
- With a selector: the value returned by
selector(state)— can be any type (primitive, object, array). - Without a selector: the full state object as returned by
storeRef.getState().
Without a Selector
When called with only a store reference,useStore returns the entire state object. The component re-renders any time any key in the store changes, regardless of which key the component actually uses.
Subscribing to the full store
Subscribing without a selector is convenient for debugging or for small stores where every key is relevant to the component. For larger stores, prefer a selector to avoid unnecessary re-renders.
With a Selector
Passing a selector limits re-renders to only the slice of state the component cares about.useStore uses Object.is to compare the previous and next selector output — if they are equal, the component is not re-rendered.
CountComponent.jsx — selecting a primitive
Stabilizing Selectors with useCallback
When you write an inline arrow function as the selector, React creates a new function reference on every render.useStore depends on selector as a useCallback dependency — a new reference causes getSnapshot to be recreated, which can trigger an extra render cycle. Stabilize the selector with useCallback, or define it as a module-level constant.
CartComponent.jsx — stable selector with useCallback
Error — Invalid Selector Type
Ifselector is provided but is not a function, useStore throws immediately with a descriptive error:
useStore(store, state.count)) instead of a selector function.
Under the Hood
useStore is implemented in fewer than 20 lines. It wraps useSyncExternalStore from React:
useStore.js
useSyncExternalStore guarantees that:
- The snapshot read during render is always consistent with what the subscription sees.
- Concurrent features like
useTransitionandSuspensedo not cause stale or torn reads. - Server-side rendering is supported (though you should provide a
getServerSnapshotif you need SSR hydration).