One hook, two shapes
One hook, two shapes in the State with useStore guide.
useStore(initial) looks at initial:
| Initial value | Result | Reading | Writing |
|---|---|---|---|
Primitive, null, undefined, function, Date, Map, Set, RegExp, class instance |
[read, write], a single cell |
count() |
setCount(5), setCount((n) => n + 1) |
Plain object (prototype Object.prototype or null) or array |
[store, setStore], a deep store |
store.user.name |
the forms in Updating a store |
A store wraps the data you pass in place and returns a read-only view of it. The view is a proxy: reading a property inside an effect, a memo or JSX subscribes to exactly that property. Nested objects and arrays are wrapped lazily as you read them.
useStore accepts { name } as a second argument to label the state in error messages and tools.