Skip to content

Jotai基礎

Jotaiは、アトミックな状態管理を提供するミニマリストなライブラリです。

Terminal window
npm install jotai
import { atom } from 'jotai';
export const countAtom = atom(0);
import { atom } from 'jotai';
import { countAtom } from './atoms';
export const doubleCountAtom = atom((get) => get(countAtom) * 2);
import { useAtom } from 'jotai';
import { countAtom, doubleCountAtom } from './atoms';
function Counter() {
const [count, setCount] = useAtom(countAtom);
const [doubleCount] = useAtom(doubleCountAtom);
return (
<div>
<p>Count: {count}</p>
<p>Double: {doubleCount}</p>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}