useUpdateAtom
引用: https://github.com/pmndrs/jotai/issues/26
对于较新版本的 Jotai,您也可以使用 useSetAtom
代替。
import { atom, useAtom } from "jotai";import { useUpdateAtom } from "jotai/utils";const countAtom = atom(0);const Counter = () => {const [count] = useAtom(countAtom);return <div>count: {count}</div>;};const Controls = () => {const setCount = useUpdateAtom(countAtom);const inc = () => setCount((c) => c + 1);return <button onClick={inc}>+1</button>;};