Gathering detailed insights and metrics for jotai
Gathering detailed insights and metrics for jotai
Gathering detailed insights and metrics for jotai
Gathering detailed insights and metrics for jotai
👻 Primitive and flexible state management for React
npm install jotai
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
18,838 Stars
1,531 Commits
622 Forks
66 Watching
8 Branches
246 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (82.24%)
JavaScript (16.53%)
CSS (1.23%)
Cumulative downloads
Total Downloads
Last day
-4%
204,071
Compared to previous day
Last week
3.2%
1,141,794
Compared to previous week
Last month
4.1%
4,857,683
Compared to previous month
Last year
122.1%
48,282,725
Compared to previous year
2
visit jotai.org or npm i jotai
Jotai scales from a simple useState replacement to an enterprise TypeScript application.
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.
1import { atom } from 'jotai' 2 3const countAtom = atom(0) 4const countryAtom = atom('Japan') 5const citiesAtom = atom(['Tokyo', 'Kyoto', 'Osaka']) 6const mangaAtom = atom({ 'Dragon Ball': 1984, 'One Piece': 1997, Naruto: 1999 })
It can be used like React.useState
:
1import { useAtom } from 'jotai' 2 3function Counter() { 4 const [count, setCount] = useAtom(countAtom) 5 return ( 6 <h1> 7 {count} 8 <button onClick={() => setCount((c) => c + 1)}>one up</button> 9 ...
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.
1const doubledCountAtom = atom((get) => get(countAtom) * 2) 2 3function DoubleCounter() { 4 const [doubledCount] = useAtom(doubledCountAtom) 5 return <h2>{doubledCount}</h2> 6}
You can combine multiple atoms to create a derived atom.
1const count1 = atom(1) 2const count2 = atom(2) 3const count3 = atom(3) 4 5const sum = atom((get) => get(count1) + get(count2) + get(count3))
Or if you like fp patterns ...
1const atoms = [count1, count2, count3, ...otherAtoms] 2const sum = atom((get) => atoms.map(get).reduce((acc, count) => acc + count))
You can make the read function an async function too.
1const urlAtom = atom('https://json.host.com') 2const fetchUrlAtom = atom(async (get) => { 3 const response = await fetch(get(urlAtom)) 4 return await response.json() 5}) 6 7function Status() { 8 // Re-renders the component after urlAtom is changed and the async function above concludes 9 const [json] = useAtom(fetchUrlAtom) 10 ...
Specify a write function at the second argument. get
will return the current
value of an atom. set
will update the value of an atom.
1const decrementCountAtom = atom( 2 (get) => get(countAtom), 3 (get, set, _arg) => set(countAtom, get(countAtom) - 1) 4) 5 6function Counter() { 7 const [count, decrement] = useAtom(decrementCountAtom) 8 return ( 9 <h1> 10 {count} 11 <button onClick={decrement}>Decrease</button> 12 ...
Just do not define a read function.
1const multiplyCountAtom = atom(null, (get, set, by) => 2 set(countAtom, get(countAtom) * by), 3) 4 5function Controls() { 6 const [, multiply] = useAtom(multiplyCountAtom) 7 return <button onClick={() => multiply(3)}>triple</button> 8}
Just make the write function an async function and call set
when you're ready.
1const fetchCountAtom = atom( 2 (get) => get(countAtom), 3 async (_get, set, url) => { 4 const response = await fetch(url) 5 set(countAtom, (await response.json()).count) 6 } 7) 8 9function Controls() { 10 const [count, compute] = useAtom(fetchCountAtom) 11 return ( 12 <button onClick={() => compute('http://count.host.com')}>compute</button> 13 ...
Jotai's fluid interface is no accident — atoms are monads, just like promises! Monads are an established pattern for modular, pure, robust and understandable code which is optimized for change. Read more about Jotai and monads.
No vulnerabilities found.
Reason
30 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 24/30 approved changesets -- score normalized to 8
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
97 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More