Jotai

Primitive and flexible state management for React

README



Jotai (light mode)
Jotai (dark mode)



npm i jotai or visit jotai.org
Build Status Build Size Version Downloads Discord Shield Open Collective

Jotai is pronounced "joe-tie" and means "state" in Japanese.

You can try live demos in the following:

How does Jotai differ from Recoil?


- Minimalistic API
- No string keys
- TypeScript oriented

First create a primitive atom


An atom represents a piece of state. All you need is to specify an initial
value, which can be primitive values like strings and numbers, objects and
arrays. You can create as many primitive atoms as you want.

  1. ```jsx
  2. import { atom } from 'jotai'

  3. const countAtom = atom(0)
  4. const countryAtom = atom('Japan')
  5. const citiesAtom = atom(['Tokyo', 'Kyoto', 'Osaka'])
  6. const mangaAtom = atom({ 'Dragon Ball': 1984, 'One Piece': 1997, Naruto: 1999 })
  7. ```

Use the atom in your components


It can be used like React.useState:

  1. ```jsx
  2. import { useAtom } from 'jotai'

  3. function Counter() {
  4.   const [count, setCount] = useAtom(countAtom)
  5.   return (
  6.     <h1>
  7.       {count}
  8.       <button onClick={() => setCount(c => c + 1)}>one up</button>
  9. ```

Create derived atoms with computed values


A new read-only atom can be created from existing atoms by passing a read
function as the first argument. get allows you to fetch the contextual value
of any atom.

  1. ```jsx
  2. const doubledCountAtom = atom((get) => get(countAtom) * 2)

  3. function DoubleCounter() {
  4.   const [doubledCount] = useAtom(doubledCountAtom)
  5.   return <h2>{doubledCount}</h2>
  6. ```

Recipes


Creating an atom from multiple atoms


You can combine multiple atoms to create a derived atom.

  1. ```jsx
  2. const count1 = atom(1)
  3. const count2 = atom(2)
  4. const count3 = atom(3)

  5. const sum = atom((get) => get(count1) + get(count2) + get(count3))
  6. ```

Or if you like fp patterns ...

  1. ```jsx
  2. const atoms = [count1, count2, count3, ...otherAtoms]
  3. const sum = atom((get) => atoms.map(get).reduce((acc, count) => acc + count))
  4. ```

### Derived async atoms needs suspense

You can make the read function an async function too.

  1. ```jsx
  2. const urlAtom = atom("https://json.host.com")
  3. const fetchUrlAtom = atom(
  4.   async (get) => {
  5.     const response = await fetch(get(urlAtom))
  6.     return await response.json()
  7.   }
  8. )

  9. function Status() {
  10.   // Re-renders the component after urlAtom changed and the async function above concludes
  11.   const [json] = useAtom(fetchUrlAtom)
  12. ```

You can create a writable derived atom


Specify a write function at the second argument. get will return the current
value of an atom. set will update an atoms value.

  1. ```jsx
  2. const decrementCountAtom = atom(
  3.   (get) => get(countAtom),
  4.   (get, set, _arg) => set(countAtom, get(countAtom) - 1),
  5. )

  6. function Counter() {
  7.   const [count, decrement] = useAtom(decrementCountAtom)
  8.   return (
  9.     <h1>
  10.       {count}
  11.       <button onClick={decrement}>Decrease</button>
  12. ```

Write only atoms


Just do not define a read function.

  1. ```jsx
  2. const multiplyCountAtom = atom(null, (get, set, by) => set(countAtom, get(countAtom) * by))

  3. function Controls() {
  4.   const [, multiply] = useAtom(multiplyCountAtom)
  5.   return <button onClick={() => multiply(3)}>triple</button>
  6. ```

Async actions


Just make the write function an async function and call set when you're ready.

  1. ```jsx
  2. const fetchCountAtom = atom(
  3.   (get) => get(countAtom),
  4.   async (_get, set, url) => {
  5.     const response = await fetch(url)
  6.     set(countAtom, (await response.json()).count)
  7.   }
  8. )

  9. function Controls() {
  10.   const [count, compute] = useAtom(fetchCountAtom)
  11.   return <button onClick={() => compute("http://count.host.com")}>compute</button>
  12. ```

Installation notes


This package requires some peer dependencies, which you need to install by
yourself.

  1. ```bash
  2. yarn add jotai react
  3. ```

Learn Jotai

free egghead Jotai introduction course by Daishi

More documents


- Basics
  - Concepts
  - Showcase
- Guides
  - Async
  - Debugging
  - Testing
  - Next.js
- API
  - Core
  - Utils
  - Devtools
  - Babel
  - SWC
- Integrations
  - Immer
  - Optics
  - Query
  - XState
  - Valtio
  - Zustand
  - Redux
  - URQL
- Advanced Recipes