new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Nuxt · API · all subjects

composables/use-state

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

useState composable signature

useState is a composable that creates a reactive and SSR-friendly shared state. It has two overloaded signatures: `export function useState<T> (init?: () => T | Ref<T>): Ref<T>` and `export function useState<T> (key: string, init?: () => T | Ref<T>): Ref<T>`. The key parameter is optional; if not provided, a key unique to the file and line number of the useState instance is generated automatically. The init parameter is a function that provides the initial state value and can return either a value of type T or a Ref<T>. The return type is always Ref<T>.

useState cannot serialize classes, functions, or symbols

The data stored in useState must be serializable to JSON. It is important that the state does not contain classes, functions, or symbols, as these cannot be serialized and will cause errors.

useState is a reserved function name

useState is a reserved function name that is transformed by the compiler. You should not name your own function useState.

useState basic usage example

const count = useState('counter', () => Math.round(Math.random() * 100)) This example creates a reactive state with the key 'counter' and initializes it to a random number between 0 and 100.

useState with shallowRef for performance

const state = useState('my-shallow-state', () => shallowRef({ deep: 'not reactive' })) You can combine useState with shallowRef to avoid deep reactivity, which improves performance when the state contains large objects and arrays. After this call, isShallow(state) returns true.

useState serialization error handling

The error 'Cannot stringify arbitrary non-POJOs' occurs when you try to store a non-serializable payload like a class instance with useState. If you need to store class instances that are not supported by Nuxt, you can use definePayloadPlugin to add a custom serializer and deserializer for your classes.

Give your agent this brain