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

useReducerAtom.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useCallback } from 'react'import { useAtom } from 'jotai'import type { PrimitiveAtom } from 'jotai'
type Scope = NonNullable<Parameters<typeof useAtom>[1]>
/* this doesn't seem to work as expected in TS4.1export function useReducerAtom<Value, Action>( anAtom: PrimitiveAtom<Value>, reducer: (v: Value) => Value): [Value, () => void]*/
export function useReducerAtom<Value, Action>( anAtom: PrimitiveAtom<Value>, reducer: (v: Value, a?: Action) => Value, scope?: Scope): [Value, (action?: Action) => void]
export function useReducerAtom<Value, Action>( anAtom: PrimitiveAtom<Value>, reducer: (v: Value, a: Action) => Value, scope?: Scope): [Value, (action: Action) => void]
export function useReducerAtom<Value, Action>( anAtom: PrimitiveAtom<Value>, reducer: (v: Value, a: Action) => Value, scope?: Scope) { const [state, setState] = useAtom(anAtom, scope) const dispatch = useCallback( (action: Action) => { setState((prev) => reducer(prev, action)) }, [setState, reducer] ) return [state, dispatch]}
jotai

Version Info

Tagged at
2 years ago