[]previous and next page

Common mistakes

Common mistakes in the State with useStore guide.

For agentsUpdated
  • Reading state once. const name = user.name in a helper function, or let 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, batch and flushSync, effects run in a microtask. See the table above.
  • Writing the store view. store.x = 1 throws. Use setStore.
  • 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. Call setStore.
  • Starting a nullable object at a plain object. useStore<User | null>({ ... }) is a deep store, so user() is not a function. Start at null, or keep the nullable part inside a store.
  • Expecting T | null to 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.