Every time you callDocumentation 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 on a NexusStore store, the store notifies all its subscribers and React schedules a re-render for any component that reads the updated key. Calling setState twice in a row on the same store therefore triggers two separate notification cycles — potentially two render passes — even though both updates are logically a single operation. setMultiple solves this by batching all changes into one atomic update: the store notifies listeners exactly once, and React renders exactly once.
The Problem with Multiple setState Calls
❌ Two setState calls — two render cycles
✅ One setMultiple call — one render cycle
Three Update Patterns
- Immer draft (simplest)
- Plain object merge
- Functional update (non-Immer)
When your store is wrapped with
immer(), pass a draft mutator function directly to setMultiple. The entire store’s current state arrives as a mutable draft — assign to any keys you want to change and leave the rest untouched.The SetMultiple component in the demo app demonstrates this pattern:src/components/SetMultiple.jsx
Replacing the Entire Store with replaceStore
By default setMultiple merges. Pass true as the second argument to replace the store entirely — the object you provide (or return from your function) becomes the complete new state.
replaceStore: true — the returned object is the new store
replaceStore pattern lets you read current values from the draft while returning a fresh object:
Immer draft + replaceStore — read draft, return new object
When to Use Each API
setState
Updating a single top-level key in the store. Works for both primitive values and nested objects (with or without Immer).
setMultiple
Updating two or more keys in the same store at once. Emits a single notification — one render cycle regardless of how many keys change.
Separate setState calls
Updating keys that live in different stores.
setMultiple only operates within one store, so you must call each store’s setState individually.| Scenario | Recommended approach |
|---|---|
| Toggle a boolean | setState with a direct value or functional update |
| Update nested object/array | setState with an Immer draft function |
| Log out and reset theme at once | setMultiple on a shared store |
| Reset entire store to defaults | setMultiple with replaceStore: true |
Update authStore and themeStore together | Separate setState on each store |