Skip to content

Commit dff4cd2

Browse files
committed
Fix nullable react-redux context references
1 parent 1d8c4b7 commit dff4cd2

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

packages/toolkit/src/dynamicMiddleware/react/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ export const createDynamicMiddleware = <
7979
// @ts-ignore
8080
context === ReactReduxContext
8181
? useDefaultDispatch
82-
: createDispatchHook(context)
82+
: createDispatchHook(
83+
context as Context<ReactReduxContextValue<
84+
State,
85+
ActionFromDispatch<Dispatch>
86+
> | null>,
87+
)
8388
function createDispatchWithMiddlewareHook<
8489
Middlewares extends Middleware<any, State, Dispatch>[],
8590
>(...middlewares: Middlewares) {

packages/toolkit/src/dynamicMiddleware/tests/react.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ describe('createReactDynamicMiddleware', () => {
8989
render(<Component />, {
9090
wrapper: ({ children }) => (
9191
<Provider store={store}>
92-
<Provider context={context} store={store2}>
92+
<Provider
93+
context={context as React.Context<ReactReduxContextValue | null>}
94+
store={store2}
95+
>
9396
{children}
9497
</Provider>
9598
</Provider>

packages/toolkit/src/query/react/ApiProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function ApiProvider<A extends Api<any, {}, any, any>>(props: {
3838
setupListeners?: Parameters<typeof setupListeners>[1] | false
3939
context?: Context<ReactReduxContextValue>
4040
}) {
41-
const context = props.context || ReactReduxContext
41+
const context = (props.context ||
42+
ReactReduxContext) as Context<ReactReduxContextValue | null>
4243
const existingContext = useContext(context)
4344
if (existingContext) {
4445
throw new Error(

packages/toolkit/src/query/tests/buildCreateApi.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from 'react-redux'
1717
import { setupApiStore, useRenderCounter } from '../../tests/utils/helpers'
1818

19-
const MyContext = React.createContext<ReactReduxContextValue>(null as any)
19+
const MyContext = React.createContext<ReactReduxContextValue | null>(null)
2020

2121
describe('buildCreateApi', () => {
2222
test('Works with all hooks provided', async () => {

0 commit comments

Comments
 (0)