-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
hooks.ts
73 lines (65 loc) · 1.66 KB
/
hooks.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {
Actions,
Thunk,
Action,
Reducer,
State,
createTypedHooks,
useStoreActions,
useStoreDispatch,
useStoreState,
useStore,
} from 'easy-peasy';
interface Model {
stateArray: Array<string>;
stateBoolean: boolean;
stateDate: Date;
stateNull: null;
stateNumber: number;
stateRegExp: RegExp;
stateString: string;
stateUndefined: undefined;
stateUnion: string | null;
actionImp: Action<Model, number>;
actionNoPayload: Action<Model>;
thunkImp: Thunk<Model, string>;
reducerImp: Reducer<number>;
nested: {
actionImp: Action<Model, number>;
thunkImp: Thunk<Model, string>;
};
}
let dispatch = useStoreDispatch();
dispatch({ type: 'FOO' });
let useStoreResult = useStoreState((state: State<Model>) => state.stateNumber);
useStoreResult + 1;
let useActionResult = useStoreActions(
(actions: Actions<Model>) => actions.actionImp,
);
useActionResult(1);
let store = useStore<Model>();
`${store.getState().stateString}world`;
const typedHooks = createTypedHooks<Model>();
useStoreResult = typedHooks.useStoreState((state) => state.stateNumber);
useStoreResult + 1;
useActionResult = typedHooks.useStoreActions((actions) => actions.actionImp);
useActionResult(1);
dispatch = typedHooks.useStoreDispatch();
dispatch({
type: 'FOO',
});
store = typedHooks.useStore();
`${store.getState().stateString}world`;
const actionNoPayload = typedHooks.useStoreActions(
(actions) => actions.actionNoPayload,
);
actionNoPayload();
typedHooks.useStoreState(
(state) => ({ num: state.stateNumber, str: state.stateString }),
(prev, next) => {
prev.num += 1;
// @ts-expect-error
prev.num += 'foo';
return prev.num === next.num;
},
);