インストール

最初にライブラリをインストールしましょう。

$ npm i jotai

使用するバージョンです。

"jotai": "^2.5.1"
"react": "^18.2.0"

公式サイトはこちら。基本チュートリアルにあるやつです。

Jotai

簡単な使い方

最初に簡単なカウントプログラムの例からみてみましょう。

  1. import { atom, useAtom } from 'jotai'
  2.  
  3. const countAtom = atom(0)
  4.  
  5. function App() {
  6. const [count, setCount] = useAtom(countAtom)
  7. const handleCountUp = () => setCount(prev => prev + 1);
  8.  
  9. return (
  10. <div>
  11. <p>{count}</p>
  12. <button onClick={handleCountUp}>+</button>
  13. </div>
  14. )
  15. }

countAtomという定数でatomを作成している以外、ほとんどuseStateと使い方は変わりませんね。

コンポーネントを分割する

Jotaiを使いたい理由としては分割したコンポーネントから使いたいからだと思いますので分けてみます。
また、useAtomをそのまま使用してもいいですが、それぞれuseSetAtom, useAtomValueという読み書き専用のフックがあるのでこちらを使ってみます。

  1. import { atom, useSetAtom, useAtomValue } from 'jotai'
  2.  
  3. const countAtom = atom(0)
  4.  
  5. function CountDisplay () {
  6. // const [count] = useAtom(countAtom)
  7. const count = useAtomValue(countAtom)
  8. return <p>{count}</p>
  9. }
  10.  
  11. function CountButton () {
  12. // const [, setCount] = useAtom(countAtom)
  13. const setCount = useSetAtom(countAtom)
  14. const handleCountUp = () => setCount(prev => prev + 1)
  15. return <button onClick={handleCountUp}>+</button>
  16. }
  17.  
  18. function App() {
  19. return (
  20. <div>
  21. <CountDisplay />
  22. <CountButton />
  23. </div>
  24. )
  25. }

読み取り専用Atom

Atomを算出して表示したとき読み取り用のAtomを作成すると便利です。
試しにカンマ区切りで表示してみましょう。

  1. const countAtom = atom(1000)
  2. const countTextAtom = atom(get => get(countAtom).toLocaleString())
  3.  
  4. function CountDisplay () {
  5. const count = useAtomValue(countTextAtom)
  6. return <p>{count}</p>
  7. }

書き込み専用Atom

読み取りとは逆に何かしらの算出をして保持するときは書き込み専用のAtomを作成します。
カウントアップの処理をAtomの処理に移行してみましょう。

  1. const countUpAtom = atom(null, (_, set) => {
  2. set(countBaseAtom, prev => prev + 1)
  3. })
  4.  
  5. function CountButton () {
  6. const setCount = useSetAtom(countUpAtom)
  7. return <button onClick={setCount}>+</button>
  8. }

読み書き込み用Atom

atomの第一引数に読み取り用、第二引数が書き込み用なのでまとめて設定することもできます。

  1. const countBaseAtom = atom(1000)
  2.  
  3. const countAtom = atom(
  4. get => get(countBaseAtom).toLocaleString(),
  5. (_, set) => {
  6. set(countBaseAtom, prev => prev + 1)
  7. }
  8. )
  9.  
  10. function CountDisplay () {
  11. const count = useAtomValue(countAtom)
  12. return <p>{count}</p>
  13. }
  14.  
  15. function CountButton () {
  16. const setCount = useSetAtom(countAtom)
  17. return <button onClick={setCount}>+</button>
  18. }

Atomを関数にまとめる

Atomを再利用する場合は関数にまとめます。

  1. import { atom, useSetAtom, useAtomValue, WritableAtom } from 'jotai'
  2.  
  3. function createCountAtoms(initialValue: number) {
  4. const baseAtom = atom(initialValue)
  5. return atom(
  6. get => get(baseAtom).toLocaleString(),
  7. (_, set) => {
  8. set(baseAtom, prev => prev + 1)
  9. }
  10. )
  11. }
  12.  
  13. const countAtom1 = createCountAtoms(0)
  14. const countAtom2 = createCountAtoms(1000)
  15.  
  16. type Props = {
  17. atom: WritableAtom<string, [], void>
  18. }
  19.  
  20. function CountDisplay({atom}: Props) {
  21. const count = useAtomValue(atom)
  22. return <p>{count}</p>
  23. }
  24.  
  25. function CountButton({atom}: Props) {
  26. const setCount = useSetAtom(atom)
  27. return <button onClick={setCount}>+</button>
  28. }
  29.  
  30. function App() {
  31. return (
  32. <div>
  33. <CountDisplay atom={countAtom1} />
  34. <CountButton atom={countAtom1} />
  35. <CountDisplay atom={countAtom2} />
  36. <CountButton atom={countAtom2} />
  37. </div>
  38. )
  39. }

以上。すごくシンプルに使えていい感じですね。