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.
createStore is the foundational factory function of NexusStore. You pass it a plain JavaScript object representing your initial state, and it returns a store object that holds that state, manages subscriptions, and exposes two update methods — setState for targeting a single top-level key and setMultiple for batching updates across many keys in a single render cycle. The returned store is framework-agnostic: it works standalone, inside React via useStore, or wrapped with immer() for mutation-style updates.
Signature
Parameters
A plain JavaScript object that defines the shape and initial values of your store. Every top-level key in this object becomes an addressable key for
setState. Nested objects are supported as values, but only top-level keys can be targeted directly.Returns
createStore returns a store object with the following properties:
Returns the current state snapshot as a plain object. The reference changes every time
setState or setMultiple produces a new state.Registers a
listener callback that is called whenever the state changes. Returns an unsubscribe function — call it to stop listening.Updates a single top-level key in the store.
selectKeyFn receives the internal keys object and must return the string key to update. nextStateOrFn is either the new value directly or a function that receives the current value and returns the next one. See setState for full documentation.Updates multiple top-level keys in a single operation, notifying all listeners exactly once.
nextStateOrFn can be a partial state object or a function. See setMultiple for full documentation.The Keys Object
Internally,createStore builds a keys object by iterating over every top-level property of initialStoreState and mapping each key to itself:
keys object is what your selectKeyFn receives inside setState. It provides autocomplete-friendly access to valid store keys, and guards against typos — if you reference a key that was not in the initial state, setState logs an error and bails out.
Critical Rule — Call Outside Components
createStore must be called once, at module scope, outside of any React component or hook. Calling it inside a component creates a brand-new store on every render, losing all accumulated state.
Code Examples
Error Handling
createStore itself never throws. However, the setState method on the returned store logs descriptive errors to the console in two situations:
First argument is not a function:
setState.
Selected key does not exist in the initial state:
selectKeyFn returns a string that was never a top-level key in initialStoreState — for example, if you mistype a key name or try to address a nested path like keys.cart.items.
Both error cases return early without modifying state or notifying listeners, so your UI remains consistent even when a bad update is attempted.