Nuestate is a reactive state container that treats the URL as the primary store. When you writeDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nuejs/nue/llms.txt
Use this file to discover all available pages before exploring further.
state.view = 'grid', the URL updates automatically. When the user hits the browser back button, state restores from the URL. Bookmarks, link sharing, and history navigation all work without any extra plumbing.
Installation
Import
state object is a Proxy over an internal API. Reading state.foo returns the current value of foo from whichever storage context it lives in. Writing state.foo = value persists the value and fires any registered listeners.
state.setup(opts)
Configure the state object before use. Defines which keys live in the URL path, the query string, session storage, or local storage. Must be called once at application startup.
An Express-style path template. Segments prefixed with
: become named route parameters. When the matching parameters change, history.pushState is called with the new path.Keys whose values are stored in the URL query string. Changing any of these keys calls
history.replaceState with the new search string.Keys persisted in
sessionStorage. Values survive page refreshes within the same browser tab but are cleared when the tab closes.Keys persisted in
localStorage. Values survive across tabs and browser restarts.Keys kept only in JavaScript memory. Values are lost on a full page reload. Useful for in-page transient state that should not appear in the URL.
When
true, attaches a global click listener to document. Clicks on <a href> elements whose pathname matches the configured route are intercepted and handled as SPA navigation — no full-page reload.Keys that can only be changed via
state.emit(). Writes via state.key = value are ignored for these keys. Useful for one-way event channels.Reading state
Usestate.key to read the current value of any configured key. The proxy reads from the appropriate storage automatically — URL params, query string, session storage, and local storage are all merged:
state.data getter returns all current state as a plain object:
Nuestate automatically coerces string values from the URL and storage.
"true" becomes true, "false" becomes false, and numeric strings become numbers. "null" and null are deleted from the state object.Writing state
Assign directly tostate.key. The proxy calls state.set() internally, persists the value to the correct store, and fires registered listeners:
state.on(events, handler)
Register a listener that fires whenever one of the named state keys changes.
A space-separated list of key names to watch. The handler fires when any of the listed keys is included in a change batch.
Called with a
changes object whose keys are the state keys that changed and whose values are the new values.state.off(events, handler)
Remove a previously registered listener.
Must match the exact string passed to
state.on.Must be the same function reference (or a function with identical source text) passed to
state.on.state.emit(name, value)
Trigger listeners for a key that is in the emit_only list, without persisting anything to storage or the URL.
The key name. Must be included in
emit_only in the setup options, otherwise this call is a no-op.The value dispatched to listeners. Not persisted anywhere.
state.set(data, is_popstate?)
Low-level batch update. Compares data against current state, persists changed keys to the appropriate stores, fires listeners for the changed keys, and pushes a new URL history entry when path or query params change.
An object of key–value pairs to apply. Only keys that are actually configured (in route, query, session, local, memory, or emit_only) and whose values differ from current values produce changes.
When
true, the URL is not updated. Used internally by the popstate event handler to restore state from history without creating a new history entry.state.init()
Fire all registered listeners with the current URL data. Call this once after registering listeners to hydrate the initial page state from the URL:
state.clear()
Reset all in-memory state and remove all registered listeners. Useful in tests or when performing a full application reset:
Browser storage layout
All session and local state is stored under a single key$state as a JSON object in the respective Storage instance:
autolink and SPA navigation
When autolink: true is set, Nuestate intercepts clicks on <a href> elements whose pathname matches the configured route pattern. Modifier keys (Meta, Ctrl) are respected — modified clicks fall through to the browser’s default behavior.
getPathData and getQueryData, the resulting state is applied via state.set(), and the browser history entry is pushed.