Jotai — Primitive and Flexible State Management for React
Jotai is an atomic state library for React. You compose atoms like React useState, and Jotai handles subscription, derivation, and async loading — all without the boilerplate of Redux or the global store of Zustand.
先审查再安装
这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install dc289209-37be-11f1-9bc6-00163e2b0d79 --target codex先 dry-run,确认写入项后再运行此命令。
What it is
Jotai is an atomic state management library for React. Instead of a single global store, you define independent atoms that hold pieces of state. Components subscribe to only the atoms they use, so re-renders are minimal. Derived atoms compute values from other atoms, and async atoms handle data fetching -- all with an API as simple as useState.
Jotai targets React developers who find Redux too heavy and Context too limited. It suits applications where fine-grained reactivity and composable state pieces matter more than a centralized store.
How it saves time or tokens
Redux requires slices, reducers, actions, selectors, and middleware for even simple state. Zustand simplifies this but still uses a single store. Jotai eliminates all boilerplate: define an atom, use it in a component with useAtom. Derived state is a function, not a selector library. The mental model is useState that works across components.
How to use
- Install Jotai:
npm install jotai. - Define atoms:
const countAtom = atom(0)and derived atoms:const doubledAtom = atom((get) => get(countAtom) * 2). - Use in components:
const [count, setCount] = useAtom(countAtom).
Example
import { atom, useAtom } from 'jotai';
const countAtom = atom(0);
const doubledAtom = atom((get) => get(countAtom) * 2);
function Counter() {
const [count, setCount] = useAtom(countAtom);
const [doubled] = useAtom(doubledAtom);
return (
<div>
<button onClick={() => setCount(c => c + 1)}>
Count: {count}, Doubled: {doubled}
</button>
</div>
);
}
Related on TokRepo
- Coding Tools -- Frontend state management libraries
- Automation Tools -- Developer workflow tools
Common pitfalls
- Creating atoms inside component render functions. Atoms should be defined at module scope or memoized. Creating them during render causes infinite re-renders.
- Not using
Providerwhen you need isolated state trees (e.g., in tests or micro-frontends). Without a Provider, all components share the default store. - Overusing derived atoms for simple transformations that could be done inline. Atoms add subscription overhead, so only extract derived atoms when multiple components need the same computation.
常见问题
Zustand uses a single store with selectors for fine-grained subscriptions. Jotai uses independent atoms with no central store. Zustand is better for teams that prefer a single source of truth with clear boundaries. Jotai is better when state is naturally decentralized and composed from many small pieces.
Yes. Define an async atom with `atom(async (get) => { ... })` and Jotai handles loading states through React Suspense. When the async atom resolves, subscribed components re-render with the fetched data.
Yes. Use `atomWithStorage` from the `jotai/utils` package. It syncs atom values to localStorage, sessionStorage, or a custom storage backend automatically. State persists across page reloads and browser tabs.
Jotai atoms use React hooks and run in Client Components. For Server Components, pass server-fetched data as props into Client Components that use Jotai for interactive state management.
Jotai's core is under 3KB gzipped. It has no dependencies beyond React. The small size makes it suitable for performance-sensitive applications where bundle size matters.
引用来源 (3)
- Jotai GitHub— Jotai provides atomic state management for React
- Jotai Documentation— Core bundle under 3KB gzipped with no external dependencies
- Jotai Utils Docs— atomWithStorage for persistent state across page reloads
讨论
相关资产
Valtio — Proxy-Based State Management for React
Valtio makes React state management simple by wrapping plain JavaScript objects in a proxy, so components automatically re-render when the properties they read change — no reducers, actions, or boilerplate required.
React Select — The Flexible Select Input for React
A highly customizable, accessible select/dropdown component for React apps with async loading, multi-select, creatable options, and full keyboard navigation.
React Select — Flexible Select Input Control for React
React Select is a flexible, accessible select input component for React with support for multi-select, async option loading, creatable options, and custom styling.
Downshift — Accessible Autocomplete and Select Primitives for React
A set of hooks and components for building accessible combobox, select, and autocomplete experiences. Handles keyboard navigation, ARIA attributes, and state management while you control the rendering.