@@ -5,11 +5,45 @@ import type { ReactReduxContextValue } from '../components/Context'
5
5
import { ReactReduxContext } from '../components/Context'
6
6
import { createStoreHook , useStore as useDefaultStore } from './useStore'
7
7
8
+ /**
9
+ * Represents a custom hook that provides a dispatch function
10
+ * from the Redux store.
11
+ *
12
+ * @template DispatchType - The specific type of the dispatch function.
13
+ *
14
+ * @since 9.1.0
15
+ * @public
16
+ */
8
17
export interface UseDispatch <
9
18
DispatchType extends Dispatch < UnknownAction > = Dispatch < UnknownAction >
10
19
> {
20
+ /**
21
+ * Returns the dispatch function from the Redux store.
22
+ *
23
+ * @returns The dispatch function from the Redux store.
24
+ *
25
+ * @template AppDispatch - The specific type of the dispatch function.
26
+ */
11
27
< AppDispatch extends DispatchType = DispatchType > ( ) : AppDispatch
12
28
29
+ /**
30
+ * Creates a "pre-typed" version of {@linkcode useDispatch useDispatch}
31
+ * where the type of the `dispatch` function is predefined.
32
+ *
33
+ * This allows you to set the `dispatch` type once, eliminating the need to
34
+ * specify it with every {@linkcode useDispatch useDispatch} call.
35
+ *
36
+ * @returns A pre-typed `useDispatch` with the dispatch type already defined.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const useAppDispatch = useDispatch.withTypes<AppDispatch>()
41
+ * ```
42
+ *
43
+ * @template OverrideDispatchType - The specific type of the dispatch function.
44
+ *
45
+ * @since 9.1.0
46
+ */
13
47
withTypes : <
14
48
OverrideDispatchType extends DispatchType
15
49
> ( ) => UseDispatch < OverrideDispatchType >
@@ -22,10 +56,15 @@ export interface UseDispatch<
22
56
* @returns {Function } A `useDispatch` hook bound to the specified context.
23
57
*/
24
58
export function createDispatchHook <
25
- S = unknown ,
26
- A extends Action = UnknownAction
59
+ StateType = unknown ,
60
+ ActionType extends Action = UnknownAction
61
+ > (
27
62
// @ts -ignore
28
- > ( context ?: Context < ReactReduxContextValue < S , A > | null > = ReactReduxContext ) {
63
+ context ?: Context < ReactReduxContextValue <
64
+ StateType ,
65
+ ActionType
66
+ > | null > = ReactReduxContext
67
+ ) {
29
68
const useStore =
30
69
context === ReactReduxContext ? useDefaultStore : createStoreHook ( context )
31
70
@@ -38,7 +77,7 @@ export function createDispatchHook<
38
77
withTypes : ( ) => useDispatch ,
39
78
} )
40
79
41
- return useDispatch as UseDispatch < Dispatch < A > >
80
+ return useDispatch as UseDispatch < Dispatch < ActionType > >
42
81
}
43
82
44
83
/**
0 commit comments