Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

👻 Primitive and flexible state management for React

License

NotificationsYou must be signed in to change notification settings

pmndrs/jotai

Repository files navigation


Jotai (light mode)Jotai (dark mode)


visitjotai.org ornpm i jotai

Build StatusBuild SizeVersionDownloadsDiscord ShieldOpen Collective

Jotai scales from a simple useState replacement to an enterprise TypeScript application.

  • Minimal core API (2kb)
  • Many utilities and extensions
  • No string keys (compared to Recoil)

Examples:Demo 1 |Demo 2

First, create a primitive atom

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

import{atom}from'jotai'constcountAtom=atom(0)constcountryAtom=atom('Japan')constcitiesAtom=atom(['Tokyo','Kyoto','Osaka'])constmangaAtom=atom({'Dragon Ball':1984,'One Piece':1997,Naruto:1999})

Use the atom in your components

It can be used likeReact.useState:

import{useAtom}from'jotai'functionCounter(){const[count,setCount]=useAtom(countAtom)return(<h1>{count}<buttononClick={()=>setCount((c)=>c+1)}>one up</button>      ...

Create derived atoms with computed values

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

constdoubledCountAtom=atom((get)=>get(countAtom)*2)functionDoubleCounter(){const[doubledCount]=useAtom(doubledCountAtom)return<h2>{doubledCount}</h2>}

Creating an atom from multiple atoms

You can combine multiple atoms to create a derived atom.

constcount1=atom(1)constcount2=atom(2)constcount3=atom(3)constsum=atom((get)=>get(count1)+get(count2)+get(count3))

Or if you like fp patterns ...

constatoms=[count1,count2,count3, ...otherAtoms]constsum=atom((get)=>atoms.map(get).reduce((acc,count)=>acc+count))

Derived async atomsneeds suspense

You can make the read function an async function too.

consturlAtom=atom('https://json.host.com')constfetchUrlAtom=atom(async(get)=>{constresponse=awaitfetch(get(urlAtom))returnawaitresponse.json()})functionStatus(){// Re-renders the component after urlAtom is changed and the async function above concludesconst[json]=useAtom(fetchUrlAtom)...

You can create a writable derived atom

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

constdecrementCountAtom=atom((get)=>get(countAtom),(get,set,_arg)=>set(countAtom,get(countAtom)-1))functionCounter(){const[count,decrement]=useAtom(decrementCountAtom)return(<h1>{count}<buttononClick={decrement}>Decrease</button>      ...

Write only derived atoms

Just do not define a read function.

constmultiplyCountAtom=atom(null,(get,set,by)=>set(countAtom,get(countAtom)*by),)functionControls(){const[,multiply]=useAtom(multiplyCountAtom)return<buttononClick={()=>multiply(3)}>triple</button>}

Async actions

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

constfetchCountAtom=atom((get)=>get(countAtom),async(_get,set,url)=>{constresponse=awaitfetch(url)set(countAtom,(awaitresponse.json()).count)})functionControls(){const[count,compute]=useAtom(fetchCountAtom)return(<buttononClick={()=>compute('http://count.host.com')}>compute</button>...

Note about functional programming

Jotai's fluid interface is no accident — atoms are monads, just like promises!Monads are anestablishedpattern for modular, pure, robust and understandable code which isoptimized for change.Read more aboutJotai and monads.

Links


[8]ページ先頭

©2009-2025 Movatter.jp