JotaiJotai

状態
Primitive and flexible state management for React

RESET

引用: https://github.com/pmndrs/jotai/issues/217

const RESET: unique symbol;

可重置原子 接受的特殊值,使用 atomWithResetatomWithDefault 或使用 atom 创建的可写原子,如果它接受 RESET 符号。

示例

import { atom } from "jotai";
import { atomWithReset, useResetAtom, RESET } from "jotai/utils";
const dollarsAtom = atomWithReset(0);
const centsAtom = atom(
(get) => get(dollarsAtom) * 100,
(get, set, newValue: number | typeof RESET) =>
set(dollarsAtom, newValue === RESET ? newValue : newValue / 100)
);
const ResetExample = () => {
const setDollars = useUpdateAtom(dollarsAtom);
const resetCents = useResetAtom(centsAtom);
return (
<>
<button onClick={() => setDollars(RESET)}>Reset dollars</button>
<button onClick={resetCents}>Reset cents</button>
</>
);
};