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 primary way to update individual keys in a NexusStore store. Every call targets exactly one top-level key and either replaces its value directly or derives the next value from the current one. NexusStore will only notify subscribers if the value actually changed.
Signature
setState signature
| Argument | Type | Description |
|---|---|---|
selectKeyFn | (keys) => string | A function that receives the store’s internal keys object and returns the key to update. |
nextStateOrFn | any | (current) => any | Either the next value directly, or an updater function that receives the current value and returns the next. |
The first argument — key-selector function
NexusStore builds akeys mirror of your initial state when createStore is called. Each key in your state object becomes a string property on keys:
How the keys object maps to your state
keys object and returns the desired key string:
Key-selector function
(keys) => keys.count instead of () => "count" gives you autocomplete support in editors and makes the reference safe to rename via automated refactoring tools.
The second argument — direct value or updater function
You can supply the new value in two ways depending on whether the next state depends on the current state.Real-world examples
CountComponent — increment and decrement
CountComponent uses the functional update pattern for both its buttons, so each click is relative to the current count regardless of how fast the user clicks:
src/components/CountComponent.jsx
ThemeComponent — direct value toggle
ThemeComponent already has the current theme value from useStore, so it calculates the next theme inline and passes it as a direct value:
src/components/ThemeComponent.jsx
AuthComponent — updater with Immer
AuthComponent updates a nested object inside the auth key. Because setState only merges at the top-level, the component uses immer’s produce as the updater function to mutate the nested value safely:
src/components/AuthComponent.jsx
Top-level keys only
setState operates exclusively on top-level keys. Attempting to target a nested path will fail because the key will not be found in the keys object.
✅ / ❌ Key-selector scope
immer’s produce, as shown in the AuthComponent example above.
Object identity check — no unnecessary re-renders
Before notifying any subscriber, NexusStore compares the current value with the next value usingObject.is. If they are the same, the update is silently dropped and no component re-renders:
Identity check inside setState (from core.js)
setState with the same primitive value twice in a row is always safe — the second call costs nothing:
Idempotent setState calls
For objects and arrays,
Object.is compares by reference. If your updater function returns a new object that is structurally equal to the previous value, NexusStore will still treat it as a change and notify subscribers. Always return the exact same reference if you intend a no-op.Error cases
NexusStore logs a console error (and returns early) in two situations:Error: non-function first argument
Error: key not found in store