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.
setState is the single-key update method on every NexusStore store. You identify the key to update through a selector function that receives the store’s internal keys object, then provide either a new value directly or a function that derives the next value from the current one. After the update, all subscribed listeners — including any useStore hooks watching that key — are notified in a single synchronous pass. When the store is wrapped with immer(), functional updaters automatically receive an Immer draft instead of the raw current value.
Signature
Parameters
A function that receives the store’s internal
keys object — a { key: 'key' } map of every top-level key in the initial state — and returns the string key to update. This indirection provides IDE autocompletion and guards against typos.The update to apply to the selected key. Two forms are accepted:
- Direct value: any value of the same type as the key’s current state. Applied as-is.
- Updater function: a function that receives the current value of the selected key and returns the next value. With an Immer-wrapped store, the function receives a mutable draft.
Top-Level Keys Only
setState can only address top-level keys of the initial state object. You cannot select a nested property by chaining keys inside the selector.
Two Update Patterns
With an Immer-Wrapped Store
When the store is created withimmer(createStore(...)), every functional updater is automatically wrapped with Immer’s produce. The function receives a mutable draft of the current value — you mutate it in place and Immer produces the next immutable state for you.
CartComponent.jsx — Immer draft on a nested object
With Plain produce (No Store Wrapping)
If you prefer not to wrap the entire store with immer(), you can apply Immer’s produce manually on a per-call basis. This is the pattern used in AuthComponent.jsx with the plain authStore:
AuthComponent.jsx — manual produce on a plain store
produce((draft) => { ... }) returns a curried function. When setState evaluates nextStateOrFn(currentState), it passes the current auth object as the base to Immer’s produced function — giving you the same draft experience without wrapping the whole store.Object Identity Check
Before notifying listeners,setState compares the current value and the next value with Object.is. If they are identical, no update is dispatched and no listeners are called. This means:
- Setting a primitive to its existing value is a no-op.
- With Immer, if the draft mutation produces no actual change, Immer returns the original reference and the update is silently skipped.
- With plain functional updates, returning the current value unchanged suppresses re-renders.
Error Cases
Both error conditions are logged toconsole.error and cause setState to return early without modifying state.
selectKeyFn is not a function:
selectKeyFn returns undefined, null, or a string that was not a key in initialStoreState — for example, if you try to address a nested path like keys.cart.items (which evaluates to the string "items", not a top-level key).