Skip to content

Commit

Permalink
Fix nullable react-redux context references
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Mar 27, 2024
1 parent 1d8c4b7 commit dff4cd2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/toolkit/src/dynamicMiddleware/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ export const createDynamicMiddleware = <
// @ts-ignore
context === ReactReduxContext
? useDefaultDispatch
: createDispatchHook(context)
: createDispatchHook(
context as Context<ReactReduxContextValue<
State,
ActionFromDispatch<Dispatch>
> | null>,
)
function createDispatchWithMiddlewareHook<
Middlewares extends Middleware<any, State, Dispatch>[],
>(...middlewares: Middlewares) {
Expand Down
5 changes: 4 additions & 1 deletion packages/toolkit/src/dynamicMiddleware/tests/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ describe('createReactDynamicMiddleware', () => {
render(<Component />, {
wrapper: ({ children }) => (
<Provider store={store}>
<Provider context={context} store={store2}>
<Provider
context={context as React.Context<ReactReduxContextValue | null>}
store={store2}
>
{children}
</Provider>
</Provider>
Expand Down
3 changes: 2 additions & 1 deletion packages/toolkit/src/query/react/ApiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function ApiProvider<A extends Api<any, {}, any, any>>(props: {
setupListeners?: Parameters<typeof setupListeners>[1] | false
context?: Context<ReactReduxContextValue>
}) {
const context = props.context || ReactReduxContext
const context = (props.context ||
ReactReduxContext) as Context<ReactReduxContextValue | null>
const existingContext = useContext(context)
if (existingContext) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/buildCreateApi.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'react-redux'
import { setupApiStore, useRenderCounter } from '../../tests/utils/helpers'

const MyContext = React.createContext<ReactReduxContextValue>(null as any)
const MyContext = React.createContext<ReactReduxContextValue | null>(null)

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

0 comments on commit dff4cd2

Please sign in to comment.