jotai
Version:
👻 Primitive and flexible state management for React
12 lines (11 loc) • 398 B
JavaScript
import { atom } from '../../vanilla.js';
import { RESET } from './constants.js';
export function atomWithReset(initialValue) {
const anAtom = atom(initialValue, (get, set, update) => {
const nextValue = typeof update === 'function'
? update(get(anAtom))
: update;
set(anAtom, nextValue === RESET ? initialValue : nextValue);
});
return anAtom;
}