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.
setMultiple lets you update any number of top-level keys in a single operation. Rather than calling setState once per key — which would notify all listeners and potentially trigger a re-render after each individual call — setMultiple computes the entire next state first and then fires listeners exactly once. This is the right tool whenever two or more keys need to change together atomically, such as clearing auth state and resetting a theme in the same user action.
Signature
Parameters
nextStateOrFn
Partial<State> | ((state: State) => Partial<State>) | ((draft: Draft<State>) => void)
required
The update to apply. Three forms are accepted depending on whether the store is Immer-wrapped:
- Plain object — merged into the current state with
{ ...state, ...nextStateOrFn }. - Function returning an object — called with the current state; the returned object is merged (or used as a replacement if
replaceStoreistrue). - Draft mutator (Immer-wrapped stores only) — called with a mutable Immer draft of the full store; mutations are applied automatically.
When
false (the default), the resolved object is shallow-merged into the existing state, preserving any keys you did not include. When true, the resolved object completely replaces the store state — any top-level key not present in the new object is permanently removed.Three Call Patterns
- Immer Draft
- Plain Object
- Functional
When the store is wrapped with
immer(), the function you pass to setMultiple receives a mutable draft of the entire store state. Mutate as many keys as you need — Immer produces the next immutable state in one shot.SetMultiple.jsx — Immer draft mutator
The replaceStore Flag
Setting replaceStore to true makes setMultiple use the resolved object as the entire new store state, replacing rather than merging.
Replace with a plain object
Replace with a functional update
Error Case — Invalid State Value
If the resolved state — whether from a direct value or a function’s return — is not a plain object (e.g. it isnull, an array, or a primitive), setMultiple logs an error and aborts without touching state:
setState Twice vs. setMultiple Once
When you need to update two keys together, prefersetMultiple over two consecutive setState calls. The difference matters for components subscribed to both keys: with separate setState calls, the component may re-render twice (once per key change); with setMultiple, it re-renders exactly once.
❌ Two setState calls — two listener notifications
✅ setMultiple — single listener notification