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)
Subscriptions
The hooks (useAtom, useAtomValue) are built on a lower-level primitive: subscriptions. When you need
to react to state changes outside a React component, persist on change, log, sync to another system, you
subscribe to the store directly.
mimir.subscribe(atomOrSelector, fn)
Registers a callback that runs whenever the atom (or selector) changes. Returns an unsubscribe function.
The callback receives the new value. It fires on every change (after set/reset actually changes the
value, no-op writes do not fire it).
Subscribing to a selector
You can subscribe to derived state too. Mimir primes the selector's dependency set on subscribe, so even if the selector was never read before, a later change to one of its (transitive) dependency atoms notifies the subscriber:
The imperative store API
mimir is the store instance. Outside React you have the full surface:
set accepts a value or an updater (prev) => next, and short-circuits when the value is unchanged
(Object.is).
When to use subscriptions
- Bridging to non-React code , update the
<html class>for theming, sync an atom to the URL, push a
value to a Web Worker.
- Side-effects on change , analytics, telemetry, autosave.
- Imperative reads in handlers , read the latest value in an event handler without making the component
subscribe.
Inside components, prefer the hooks, they handle subscribe/unsubscribe with the component lifecycle for you.
Reach for subscribe when there is no component to hang the lifecycle on.
Lifecycle and cleanup
Always call the returned unsubscribe when the subscription's owner goes away, otherwise the callback (and anything it closes over) leaks. In a React effect:
(That is essentially what useAtomValue does internally, this is the manual version for cases the hook does
not cover.)
How the hooks use it
For context, the client useAtom is roughly:
So "a component re-renders when an atom changes" is just a subscription whose callback calls setVal. The
store is the source of truth; React is one of its subscribers.
Next: the server/client handoff, SSR & hydration.