-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.ts
43 lines (35 loc) · 1006 Bytes
/
hook.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
/* eslint-disable */
import {
ActionCreator,
ActionCreatorsMapObject,
AsyncThunk,
bindActionCreators
} from '@reduxjs/toolkit'
import { useMemo } from 'react'
import { useDispatch } from 'react-redux'
import { AppDispatch, ThunkConfig } from './types'
export const useAppDispatch = useDispatch<AppDispatch>
type BoundActions<Actions extends ActionCreatorsMapObject> = {
[key in keyof Actions]: Actions[key] extends AsyncThunk<
any,
any,
ThunkConfig<any>
>
? BoundAsyncThunk<Actions[key]>
: Actions[key]
}
type BoundAsyncThunk<Action extends ActionCreator<any>> = (
...args: Parameters<Action>
) => ReturnType<ReturnType<Action>>
export const useActionCreatorsTyped = <
Actions extends ActionCreatorsMapObject = ActionCreatorsMapObject
>(
actions: Actions
): BoundActions<Actions> => {
const dispatch = useAppDispatch()
const memoizedActions = useMemo(
() => bindActionCreators(actions, dispatch),
[actions, dispatch]
)
return memoizedActions
}