Documentation
Docs
Introduction
Getting Started
Tutorial: build an app
Core Concepts
Routing
Server-Side Rendering
Mimir, state management
Pages & Layouts
API Routes
Styling & Theming
Building & Deploying
API Reference
Guides
Recipes
FAQ (use cases)
Reading and writing
Three hooks cover every component interaction with an atom: useAtom (read + write), useAtomValue
(read-only), and useSetAtom (write-only). Outside React, the mimir store offers get/set/reset.
useAtom , read and write
Coming from React useState? useAtom is the same API. It returns the exact same [value, setValue]
tuple, setValue takes a value or an (prev) => next updater, and it triggers a re-render, just like
useState. The only differences: the atom is declared once, outside the component (so the state is
shared across components and survives navigation/reload when persist: true), and you pass that
atom to the hook:
Returns [value, setValue], like useState, but the state lives in the store:
Reading subscribes this component to the atom: when anyone sets themeAtom, every component using it
re-renders with the new value. There is no Provider and no prop drilling, distant components stay in sync
because they read the same store slot.
Updater functions
setValue accepts a value or an updater (prev) => next, use the updater when the new value depends on
the old:
useAtomValue , read-only
When a component only reads (it never writes), useAtomValue returns the value directly. It also accepts a
selector (derived state):
It subscribes the same way useAtom does, the component re-renders when the value changes.
useSetAtom , write-only
When a component only writes and does not need to re-render on changes (a button buried in a toolbar),
useSetAtom returns just the setter. The component does not subscribe, so it will not re-render when the
atom changes elsewhere, a small but real performance win for write-only widgets:
useSetAtom only accepts an atom (not a selector), you cannot set derived state.
Choosing a hook
| You need to... | Use |
|---|---|
| Read and write, re-render on change | useAtom |
| Read only, re-render on change | useAtomValue |
| Read derived state | useAtomValue(selector) |
| Write only, no re-render | useSetAtom |
Immutability
Treat atom values as immutable. To update an object or array, produce a new value rather than mutating
the old one, Mimir compares with Object.is, so a mutated-in-place object looks unchanged and will not
notify subscribers:
Outside React , the mimir store
For logic that is not a component (an event handler in a module, a startup routine, an API-driven update), use the store directly:
set/reset notify subscribers and dependent selectors exactly as the hooks do, so a non-component write
still re-renders the components that read the atom.
No update when nothing changed
set and reset short-circuit when the next value is Object.is-equal to the current one, no notification,
no re-render. This keeps updates cheap and avoids spurious renders when you "set" a value to what it already
was.
Next: computing values from atoms, Selectors.