Every piece of shared state in NexusStore starts with a call toDocumentation 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.
createStore. You hand it a plain JavaScript object describing your initial state, and it returns a store object with everything you need to read, update, and subscribe to that state — no classes, no decorators, no boilerplate.
The createStore function
createStore accepts a single argument: a plain JavaScript object that becomes the initial state of the store. It returns a store object that exposes four methods.
Full return shape of createStore
| Method | Signature | Description |
|---|---|---|
getState | () => state | Returns the current state object. |
subscribe | (listener: () => void) => () => void | Adds a listener that fires on every state change. Returns a cleanup function. |
setState | (selectKeyFn, nextStateOrFn) | Updates a single top-level key. See Updating State. |
setMultiple | (nextStateOrFn, replaceStore?) | Merges (or replaces) multiple keys in one call. |
Creating your first store
The simplest possible store holds a single counter value:countStore.js
How NexusStore builds the keys object
When you callcreateStore, NexusStore internally builds a keys mirror of your initial state object. Each key in the initial state becomes a matching string property on keys:
Internal keys object (built from initial state)
keys object is passed to the selector function you provide to setState, giving you a type-safe way to reference state keys without hardcoding strings:
Using keys in setState
The
keys object is an internal implementation detail. You interact with it only through the selector function you pass to setState — NexusStore handles the rest.Declaring stores outside components
React re-renders components frequently — on prop changes, context updates, parent re-renders, and more. Any variable declared inside a component is re-created on each of those cycles.❌ Wrong — store is recreated on every render
✅ Correct — store lives outside the component
CountComponent in the source repo follows exactly this pattern — the store is declared at module scope, above the component definition:
src/components/CountComponent.jsx
Using multiple stores
As your application grows, you will naturally group related state into separate, focused stores. Thesrc/store/store.js file in the source repo demonstrates this pattern clearly:
src/store/store.js
store.js file keeps your state definitions central and easy to find from any component.
Where to define your stores
There are two common patterns for organising store definitions:Dedicated store file
Define all stores in a shared
src/store/store.js (or src/stores/) file and export them. Best for stores used by multiple components or large applications.Co-located with a component
Define the store at the top of the component’s own file (above the component function). Best for stores that are genuinely private to a single component — like
countStore in CountComponent.jsx.TypeScript: annotating initial state
NexusStore’s source is plain JavaScript, but you can layer TypeScript types onto your stores for full type safety by typing the initial state object before passing it tocreateStore:
Typed store example
createStore is generic-friendly, TypeScript will infer the state shape from the typed object you provide — no additional type parameters required.