Overview
memo(fn) creates a reactive computed atom. fn runs immediately; atoms read inside become dependencies; whenever they change, fn re-runs and the returned atom updates.
The return value is a full AtomType<T> — callable, subscribable, and chainable. It is functionally identical to atom(() => …) (implemented separately to avoid a circular import). Prefer memo at module level or when the call site should read as “computed”; prefer atom(fn) for terse inline derived values in components.
Create memos in the component outer function or at module level — never inside the template return, and never inside another memo/effect callback.
memo(fn) vs atom(() => …)
|
Form
|
Behavior
|
Prefer when
|
|---|---|---|
memo(fn)
| Same reactive runner | Module-level or shared derived values — clearer “computed” intent. |
atom(() => expr)
| Functionally identical | Inline derived atoms inside component outer functions (slightly terser). |
Derived values with memo()
memo() computes from tracked atoms and only re-runs when a dependency changes — not on every render.
memo() vs atom(() => …)
Both forms share the same runner. Values stay in lockstep — choose the call-site style you prefer.