Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dvlkit/nuxe/llms.txt
Use this file to discover all available pages before exploring further.
useState is Nuxe’s answer to shared reactive state that works across the SSR–client boundary. Unlike a plain ref defined in a module, a useState value lives inside the Nuxe app payload — the same object that carries useAsyncData results from server to client. This means the value is serialized into the HTML by the server, restored on the client before the first render, and kept in sync across every component that calls useState with the same key.
Two components that call
useState('count') receive the same Ref. Mutating it in one component is immediately visible in the other — no Pinia store or provide/inject boilerplate required.Signature
Parameters
A unique string that identifies this state entry within the app payload. Must be a non-empty string —
useState throws a TypeError if key is absent, empty, or not a string.An optional factory function called once to produce the initial value when the key does not yet exist in the payload. Subsequent calls with the same key return the existing
Ref without calling init again. useState throws if init is provided but is not a function.Return Values
A reactive
Ref backed by the app payload. Reading .value returns the current state. Writing .value updates it reactively everywhere the same key is used.Error conditions
useState throws in three situations:
| Condition | Error type | Message |
|---|---|---|
key is not a non-empty string | TypeError | [nuxe] [useState] key must be a non-empty string |
init is provided but is not a function | Error | [nuxe] [useState] init must be a function |
| Called outside a Nuxe plugin or setup function | Error | [nuxe] useState() must be called inside a Nuxe plugin or setup function… |
createNuxeState
Creates an initial NuxeState record (a Record<string, Ref<unknown>>) from a plain object. Each value is wrapped in a ref unless it is already a Ref.
Signature
Example
clearNuxeState
Removes one or more keys from the app state payload. Accepts a single key string, an array of keys, or a predicate function. If called with no argument, every key is removed.
Signature
string— remove that single key.string[]— remove all listed keys.(key: string) => boolean— remove every key for which the predicate returnstrue.- omitted — remove all keys.
Examples
Basic counter shared across components
useState('counter') receives the same Ref and will react to changes.