Common mistakes
Common mistakes in the State with useStore guide.
- Reading state once.
const name = user.namein a helper function, orlet n = count()in a plain function, captures a value. Read inside JSX, an effect, a memo or a derived const of a component. - Expecting the DOM to be current after a write. Outside an event handler,
batchandflushSync, effects run in a microtask. See the table above. - Writing the store view.
store.x = 1throws. UsesetStore. - Keeping a draft. Use the draft only while the update function runs.
- Mutating the object you passed to
useStore. The store wraps it in place, but writes to the raw object bypass notification. CallsetStore. - Starting a nullable object at a plain object.
useStore<User | null>({ ... })is a deep store, souser()is not a function. Start atnull, or keep the nullable part inside a store. - Expecting
T | nullto merge. A union value is replaced as a whole by its setter; to merge into data, hold the data in a store and keep the nullable part inside it. - Assigning an array built from draft items. See Rules of the draft.
- Module-level state on the server. One copy serves all requests.