Skip to content

Commit 4a1857c

Browse files
committed
Update useDispatch types to accept a generic arg
1 parent 7b62c14 commit 4a1857c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/components/Context.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import React from 'react'
22
import { Action, AnyAction, Store } from 'redux'
3-
import type { FixTypeLater } from '../types'
43
import type { Subscription } from '../utils/Subscription'
54

65
export interface ReactReduxContextValue<
7-
SS = FixTypeLater,
6+
SS = any,
87
A extends Action = AnyAction
98
> {
109
store: Store<SS, A>
1110
subscription: Subscription
1211
}
1312

1413
export const ReactReduxContext =
15-
/*#__PURE__*/ React.createContext<ReactReduxContextValue | null>(null)
14+
/*#__PURE__*/ React.createContext<ReactReduxContextValue>(null as any)
1615

1716
export type ReactReduxContextInstance = typeof ReactReduxContext
1817

src/hooks/useDispatch.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
import { ReactReduxContext } from '../components/Context'
1+
import { Action, ActionCreator, AnyAction, Dispatch, Store } from 'redux'
2+
import { Context } from 'react'
3+
4+
import {
5+
ReactReduxContext,
6+
ReactReduxContextValue,
7+
} from '../components/Context'
28
import { useStore as useDefaultStore, createStoreHook } from './useStore'
9+
import { RootStateOrAny } from '../types'
310

411
/**
512
* Hook factory, which creates a `useDispatch` hook bound to a given context.
613
*
714
* @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
815
* @returns {Function} A `useDispatch` hook bound to the specified context.
916
*/
10-
export function createDispatchHook(context = ReactReduxContext) {
17+
export function createDispatchHook<
18+
S = RootStateOrAny,
19+
A extends Action = AnyAction
20+
// @ts-ignore
21+
>(context?: Context<ReactReduxContextValue<S, A>> = ReactReduxContext) {
1122
const useStore =
23+
// @ts-ignore
1224
context === ReactReduxContext ? useDefaultStore : createStoreHook(context)
1325

14-
return function useDispatch() {
26+
return function useDispatch<
27+
AppDispatch extends Dispatch<A> = Dispatch<A>
28+
>(): AppDispatch {
1529
const store = useStore()
30+
// @ts-ignore
1631
return store.dispatch
1732
}
1833
}

0 commit comments

Comments
 (0)