Nuxe providesDocumentation 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 — a composable that creates reactive refs backed by the application payload. State set on the server is automatically serialized into window.__NUXE__ and restored on the client, making it available across components without a separate state management library. Every call with the same key returns the exact same Ref, so components always stay in sync.
useState
Signature
A unique string identifier for this piece of state. The same key always returns the same
Ref within an application instance.Optional factory function called once to create the initial value when the key is first registered. Subsequent calls with the same key ignore
init and return the existing ref.Example
The recommended pattern is to wrapuseState in a dedicated composable so the key string is co-located with its consumers:
useCounter() receives the same underlying ref. Updating count.value in one component immediately reflects in all others.
SSR and hydration
During server-side rendering,useState stores its value in the server payload under the given key. When the HTML reaches the browser, Nuxe deserializes the payload from window.__NUXE__ and populates the state registry before Vue mounts. The client never starts with a blank slate — the exact server value is available synchronously on first render, preventing hydration mismatches.
Server render
useState('counter', () => 0) is called; the initial value 0 is stored in the server payload.HTML is sent
The serialized payload is inlined into the HTML as
window.__NUXE__ = { state: { counter: 0 } }.Error conditions
useState throws in three situations:
- Key is not a non-empty string — throws
TypeError: [nuxe] [useState] key must be a non-empty string. initis provided but is not a function — throwsError: [nuxe] [useState] init must be a function.- Called outside a Nuxe context — throws
Error: [nuxe] useState() must be called inside a Nuxe plugin or setup function, or inside a runWithContext that has provided an app via provideNuxeApp().
createNuxeState
createNuxeState is a utility for building an initial state object outside of a component. It converts plain values into Refs and is useful for seeding state in plugins or server middleware.
Ref, it is kept as-is.
clearNuxeState
clearNuxeState removes entries from the state registry. It is useful in tests, after logout flows, or whenever you need to reset a slice of shared state.
Signatures
Examples
Wrap
useState in a named composable (e.g. useCounter) rather than calling it with a raw string in every component. This keeps the key string co-located with the state definition and makes global search-and-replace safe if you need to rename a key.