Skip to content

Commit 0e60aa7

Browse files
committed
Add JSDocs for useDispatch
1 parent 0f65c2a commit 0e60aa7

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/hooks/useDispatch.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,45 @@ import type { ReactReduxContextValue } from '../components/Context'
55
import { ReactReduxContext } from '../components/Context'
66
import { createStoreHook, useStore as useDefaultStore } from './useStore'
77

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+
*/
817
export interface UseDispatch<
918
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>
1019
> {
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+
*/
1127
<AppDispatch extends DispatchType = DispatchType>(): AppDispatch
1228

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+
*/
1347
withTypes: <
1448
OverrideDispatchType extends DispatchType
1549
>() => UseDispatch<OverrideDispatchType>
@@ -22,10 +56,15 @@ export interface UseDispatch<
2256
* @returns {Function} A `useDispatch` hook bound to the specified context.
2357
*/
2458
export function createDispatchHook<
25-
S = unknown,
26-
A extends Action = UnknownAction
59+
StateType = unknown,
60+
ActionType extends Action = UnknownAction
61+
>(
2762
// @ts-ignore
28-
>(context?: Context<ReactReduxContextValue<S, A> | null> = ReactReduxContext) {
63+
context?: Context<ReactReduxContextValue<
64+
StateType,
65+
ActionType
66+
> | null> = ReactReduxContext
67+
) {
2968
const useStore =
3069
context === ReactReduxContext ? useDefaultStore : createStoreHook(context)
3170

@@ -38,7 +77,7 @@ export function createDispatchHook<
3877
withTypes: () => useDispatch,
3978
})
4079

41-
return useDispatch as UseDispatch<Dispatch<A>>
80+
return useDispatch as UseDispatch<Dispatch<ActionType>>
4281
}
4382

4483
/**

test/typetests/hooks.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ function testShallowEqual() {
8484
)
8585
expectExactType<string>(selected1)
8686

87-
// const useAppSelector: UseSelector<TestState> = useSelector
8887
const useAppSelector = useSelector.withTypes<TestState>()
8988

9089
const selected2 = useAppSelector((state) => state.stateProp, shallowEqual)

0 commit comments

Comments
 (0)