Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork696
👻 Primitive and flexible state management for React
License
pmndrs/jotai
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
visitjotai.org ornpm i jotai
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)
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})
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> ...
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>}
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))
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)...
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> ...
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>}
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>...
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.
About
👻 Primitive and flexible state management for React
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.

