Skip to main content
Module

x/jotai/src/utils/atomWithReset.ts

👻 Primitive and flexible state management for React
Go to Latest
File
import { atom } from 'jotai'import type { SetStateAction, WritableAtom } from 'jotai'import { RESET } from './constants'
export function atomWithReset<Value>(initialValue: Value) { type Update = SetStateAction<Value> | typeof RESET const anAtom = atom<Value, Update>(initialValue, (get, set, update) => { if (update === RESET) { set(anAtom, initialValue) } else { set( anAtom, typeof update === 'function' ? (update as (prev: Value) => Value)(get(anAtom)) : update ) } }) return anAtom as WritableAtom<Value, Update>}