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.
immer() is a thin wrapper that enhances an existing NexusStore store with automatic Immer integration. Once wrapped, any functional updater you pass to setState or setMultiple receives a mutable Immer draft instead of the raw current value — you write mutations directly against the draft and Immer produces the next immutable state behind the scenes. The wrapped store is otherwise identical: it has the same getState, subscribe, setState, and setMultiple interface and can be used with useStore without any changes.
Signature
Parameters
The store object returned by
createStore. Pass the raw store — do not pre-wrap it or call immer() more than once on the same store instance.Returns
A new store object with the samegetState, subscribe, setState, and setMultiple interface, where:
setState: ifnextStateOrFnis a function, it is wrapped with Immer’sproducebefore being forwarded to the underlying store’ssetState. Direct (non-function) values are passed through unchanged.setMultiple: ifnextStateOrFnis a function, it is wrapped with Immer’sproducebefore being forwarded to the underlying store’ssetMultiple. Plain objects are passed through unchanged.getStateandsubscribeare forwarded directly from the original store — no wrapping.
Full Source
The implementation ofimmer() is intentionally minimal:
NexusStore/immer.js
produce(nextStateOrFn) returns a curried producer — a new function that accepts the current state, runs your mutator against a draft copy, and returns the next immutable state. That curried producer is what gets forwarded to the underlying store methods, so the store’s existing Object.is identity check and listener-notification logic work exactly as they do without Immer.
Creation Pattern
Callimmer() immediately after createStore, at module scope, and export the wrapped result:
store/store.js
Behavior Comparison
| Call pattern | With immer() | Without immer() |
|---|---|---|
Direct value — store.setState((k) => k.theme, "dark") | Works normally — value passed through | Works normally — value passed through |
Functional setState — store.setState((k) => k.cart, (draft) => { draft.items.push("Kiwi") }) | Immer draft ✅ — mutate directly | Plain value ✅ — must return new object |
setMultiple((draft) => { draft.auth.isLoggedin = false }) | Immer draft ✅ — mutate directly | Plain object function — must return partial object |
CartComponent vs. AuthComponent — Side-by-Side
These two components update similar nested state but use different strategies.CartComponent uses the Immer-wrapped store and mutates drafts; AuthComponent uses the plain authStore and applies produce manually per call.
Prerequisite — Install Immer
immer() imports produce from the immer package. You must install it separately — it is not bundled with NexusStore:
Install Immer