Jotai + Next.js
Purpose
Use this skill to design, implement, debug, and migrate Jotai state in React/Next.js apps with version-aware guidance.
Version baseline (re-check when invoked)
- Reference package version:
jotai@2.18.0 (npm latest, verified on 2026-02-22).
- Latest release reviewed:
v2.18.0 (published on 2026-02-19).
- Re-check commands:
npm view jotai version
npm view jotai dist-tags --json
npm view jotai time.modified
https://github.com/pmndrs/jotai/releases
When to use
- SSR/CSR state architecture in Next.js (App Router or Pages Router).
- Atom modeling (core), utility selection (
storage, select, async, resettable, family).
- Migration work for v2+ and upcoming v3 deprecations.
- Performance/debugging issues caused by atom identity or over-rendering.
Non-negotiable rules
- For SSR, wrap the app/subtree in
<Provider> to avoid shared global store across requests.
- Use
useHydrateAtoms in client components ('use client'), not server components.
- Atoms hydrate once per store; do not expect rerender-time hydration to overwrite values.
- Avoid returning unresolved promises during SSR paths; prefetch and hydrate when possible.
- Keep atom references stable. Do not create raw
atom(...) in render without useMemo or useRef.
- Prefer
useSetAtom when write-only to reduce unnecessary rerenders.
- Treat
selectAtom as an escape hatch; keep both the base atom and selector reference stable.
atomFamily from jotai/utils is deprecated and planned for removal in v3; prefer jotai-family.
jotai/babel is deprecated; use jotai-babel.
@swc-jotai/* plugins are experimental; use only when the project accepts experimental compiler behavior.
Quick snippets
// app/providers.tsx
'use client'
import { Provider } from 'jotai'
export function Providers({ children }: { children: React.ReactNode }) {
return <Provider>{children}</Provider>
}
// app/page.tsx (client hydration entry)
'use client'
import { atom } from 'jotai'
import { useHydrateAtoms } from 'jotai/utils'
const countAtom = atom(0)
export function HydrateCount({ initial }: { initial: number }) {
useHydrateAtoms([[countAtom, initial]])
return null
}
// stable selectAtom usage
const selectedAtom = useMemo(
() => selectAtom(baseAtom, (s) => s.slice),
[baseAtom],
)
Workflow
1) Confirm app/runtime boundaries
- Identify Next.js router mode (
app/ vs pages/) and where client boundaries exist.
- Find atom definitions and where providers/stores are mounted.
2) Establish store strategy
- Default: one
<Provider> at app root for SSR safety.
- Use custom stores (
createStore) when scoping state to subtrees/tests or outside React via store API.
3) Hydration and async strategy
- Hydrate server-fetched values with
useHydrateAtoms.
- For multiple stores, hydrate each store explicitly.
- If async atoms feed sync-only utilities (
splitAtom, etc.), unwrap/load before composition.
4) Utility and recipe selection
atomWithStorage for persistence; account for SSR mismatch and getOnInit.
resettable APIs when reset semantics are explicit (RESET, useResetAtom).
selectAtom only for equality-based slicing that pure derived atoms cannot handle.
atomWithDebounce, useAtomEffect, and focusAtom/splitAtom/selectAtom patterns for advanced workflows.
5) Migration and release checks
- Scan recent releases for deprecations/internal changes before recommending imports.
- Call out migrations explicitly when touching Babel tooling,
atomFamily, or deprecated async helpers.
6) Deliverables
Return:
- Suggested atom/store architecture.
- Exact API choices and why.
- Migration edits (before/after snippets).
- Risks and test checklist (SSR hydration, rerender behavior, stale identity).
Progressive disclosure
Load only what is needed:
reference/nextjs-ssr-playbook.md for Next.js setup and hydration.
reference/core-utilities-recipes.md for API and recipe choices.
reference/latest-version-and-changelog.md for version-aware migration guidance.
reference/sources.md for canonical links and verification trail.
1---2name: jotai-nextjs3description: Expert workflow for Jotai state management with a Next.js focus, covering core APIs, SSR hydration, utilities, recipes, and current release migrations.4---56# Jotai + Next.js78## Purpose910Use this skill to design, implement, debug, and migrate Jotai state in React/Next.js apps with version-aware guidance.1112## Version baseline (re-check when invoked)1314- Reference package version: `jotai@2.18.0` (npm `latest`, verified on 2026-02-22).15- Latest release reviewed: `v2.18.0` (published on 2026-02-19).16- Re-check commands:17 - `npm view jotai version`18 - `npm view jotai dist-tags --json`19 - `npm view jotai time.modified`20 - `https://github.com/pmndrs/jotai/releases`2122## When to use2324- SSR/CSR state architecture in Next.js (App Router or Pages Router).25- Atom modeling (core), utility selection (`storage`, `select`, `async`, `resettable`, `family`).26- Migration work for v2+ and upcoming v3 deprecations.27- Performance/debugging issues caused by atom identity or over-rendering.2829## Non-negotiable rules3031- For SSR, wrap the app/subtree in `<Provider>` to avoid shared global store across requests.32- Use `useHydrateAtoms` in client components (`'use client'`), not server components.33- Atoms hydrate once per store; do not expect rerender-time hydration to overwrite values.34- Avoid returning unresolved promises during SSR paths; prefetch and hydrate when possible.35- Keep atom references stable. Do not create raw `atom(...)` in render without `useMemo` or `useRef`.36- Prefer `useSetAtom` when write-only to reduce unnecessary rerenders.37- Treat `selectAtom` as an escape hatch; keep both the base atom and selector reference stable.38- `atomFamily` from `jotai/utils` is deprecated and planned for removal in v3; prefer `jotai-family`.39- `jotai/babel` is deprecated; use `jotai-babel`.40- `@swc-jotai/*` plugins are experimental; use only when the project accepts experimental compiler behavior.4142## Quick snippets4344```tsx45// app/providers.tsx46'use client'47import { Provider } from 'jotai'48export function Providers({ children }: { children: React.ReactNode }) {49 return <Provider>{children}</Provider>50}51```5253```tsx54// app/page.tsx (client hydration entry)55'use client'56import { atom } from 'jotai'57import { useHydrateAtoms } from 'jotai/utils'58const countAtom = atom(0)59export function HydrateCount({ initial }: { initial: number }) {60 useHydrateAtoms([[countAtom, initial]])61 return null62}63```6465```tsx66// stable selectAtom usage67const selectedAtom = useMemo(68 () => selectAtom(baseAtom, (s) => s.slice),69 [baseAtom],70)71```7273## Workflow7475### 1) Confirm app/runtime boundaries7677- Identify Next.js router mode (`app/` vs `pages/`) and where client boundaries exist.78- Find atom definitions and where providers/stores are mounted.7980### 2) Establish store strategy8182- Default: one `<Provider>` at app root for SSR safety.83- Use custom stores (`createStore`) when scoping state to subtrees/tests or outside React via store API.8485### 3) Hydration and async strategy8687- Hydrate server-fetched values with `useHydrateAtoms`.88- For multiple stores, hydrate each store explicitly.89- If async atoms feed sync-only utilities (`splitAtom`, etc.), unwrap/load before composition.9091### 4) Utility and recipe selection9293- `atomWithStorage` for persistence; account for SSR mismatch and `getOnInit`.94- `resettable` APIs when reset semantics are explicit (`RESET`, `useResetAtom`).95- `selectAtom` only for equality-based slicing that pure derived atoms cannot handle.96- `atomWithDebounce`, `useAtomEffect`, and `focusAtom/splitAtom/selectAtom` patterns for advanced workflows.9798### 5) Migration and release checks99100- Scan recent releases for deprecations/internal changes before recommending imports.101- Call out migrations explicitly when touching Babel tooling, `atomFamily`, or deprecated async helpers.102103### 6) Deliverables104105Return:106107- Suggested atom/store architecture.108- Exact API choices and why.109- Migration edits (before/after snippets).110- Risks and test checklist (SSR hydration, rerender behavior, stale identity).111112## Progressive disclosure113114Load only what is needed:115116- `reference/nextjs-ssr-playbook.md` for Next.js setup and hydration.117- `reference/core-utilities-recipes.md` for API and recipe choices.118- `reference/latest-version-and-changelog.md` for version-aware migration guidance.119- `reference/sources.md` for canonical links and verification trail.