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
Typescript
Module System
Min. Node Version
Node Version
NPM Version
93.3
Supply Chain
97.1
Quality
92.2
Maintenance
100
Vulnerability
100
License
TypeScript (82.29%)
JavaScript (16.48%)
CSS (1.23%)
Total Downloads
79,310,357
Last Day
203,059
Last Week
1,111,966
Last Month
4,811,747
Last Year
49,128,829
18,944 Stars
1,539 Commits
630 Forks
66 Watching
9 Branches
247 Contributors
Minified
Minified + Gzipped
Latest Version
2.10.3
Package Id
jotai@2.10.3
Unpacked Size
428.18 kB
Size
76.98 kB
File Count
204
NPM Version
10.7.0
Node Version
18.20.4
Publised On
20 Nov 2024
Cumulative downloads
Total Downloads
Last day
-3%
203,059
Compared to previous day
Last week
4.2%
1,111,966
Compared to previous week
Last month
-0.1%
4,811,747
Compared to previous month
Last year
116%
49,128,829
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
license file detected
Details
Reason
no binaries found in the repo
Reason
packaging workflow detected
Details
Reason
Found 16/30 approved changesets -- score normalized to 5
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is not fuzzed
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
98 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-02
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