Does useState() a default value when the state key already set ?
The doc is unclear on the topic and my tests show contradictory results.
Let's say I call a component multiple times with
will the second useState() be reset to
{user: null}
then to {user: 'bar'}
or is useState smart enough to know it aleady has a value and to not reinstantiate itself ?14 Replies
The latter
If you’re updating states in a test, you’d usually want to await a
nextTick
so useState's default for key 'foo' will strictly be set if 'foo' doesn't exist ?
Correct
I don't think it's explained in the docs that's a pity
but thanks a lot !
it's even more confusing when they suggest to use composable https://nuxt.com/docs/getting-started/state-management#shared-state
Nuxt
State Management · Get Started with Nuxt
Nuxt provides powerful state management libraries and the useState composable to create a reactive and SSR-friendly shared state.
it might imply that it won't work outside a composable
but alright, that's all clear now thanks again
Alright 👍
if you allow me one very last question : is it bad to use useState in a loop ?
where it might be checked a hundred times ?
Oh it’s fine
It’s just a state. The only difference between that and a
ref
is that it’s globally accessibleand a third thanks to you sir
You’re welcome
or lady
or person
🙂
😂
She’s a he 🙃
PR welcome!