deno.land / x / jotai@v1.8.4 / src / utils / atomWithReset.ts

atomWithReset.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { atom } from 'jotai'import type { WritableAtom } from 'jotai'import { RESET } from './constants'
type SetStateActionWithReset<Value> = | Value | typeof RESET | ((prev: Value) => Value | typeof RESET)
export function atomWithReset<Value>(initialValue: Value) { type Update = SetStateActionWithReset<Value> const anAtom = atom<Value, Update>(initialValue, (get, set, update) => { const nextValue = typeof update === 'function' ? (update as (prev: Value) => Value | typeof RESET)(get(anAtom)) : update
set(anAtom, nextValue === RESET ? initialValue : nextValue) }) return anAtom as WritableAtom<Value, Update>}
jotai

Version Info

Tagged at
2 years ago