Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error if ApiProvider is nested inside a normal Provider. #3855

Merged
merged 2 commits into from Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/toolkit/src/query/react/ApiProvider.tsx
@@ -1,9 +1,10 @@
import { configureStore } from '@reduxjs/toolkit'
import type { Context } from 'react'
import { useContext } from 'react'
import { useEffect } from 'react'
import React from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import { Provider } from 'react-redux'
import { Provider, ReactReduxContext } from 'react-redux'
import { setupListeners } from '@reduxjs/toolkit/query'
import type { Api } from '@reduxjs/toolkit/query'

Expand Down Expand Up @@ -37,6 +38,13 @@ 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 existingContext = useContext(context)
if (existingContext) {
throw new Error(
'Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup.'
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
)
}
const [store] = React.useState(() =>
configureStore({
reducer: {
Expand All @@ -55,7 +63,7 @@ export function ApiProvider<A extends Api<any, {}, any, any>>(props: {
)

return (
<Provider store={store} context={props.context}>
<Provider store={store} context={context}>
{props.children}
</Provider>
)
Expand Down
13 changes: 13 additions & 0 deletions packages/toolkit/src/query/tests/apiProvider.test.tsx
Expand Up @@ -2,6 +2,8 @@ import * as React from 'react'
import { createApi, ApiProvider } from '@reduxjs/toolkit/query/react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import { waitMs } from './helpers'
import { Provider } from 'react-redux'
import { configureStore } from '@reduxjs/toolkit'

const api = createApi({
baseQuery: async (arg: any) => {
Expand Down Expand Up @@ -57,4 +59,15 @@ describe('ApiProvider', () => {
// Being that nothing has changed in the args, this should never fire.
expect(getByTestId('isFetching').textContent).toBe('false')
})
test('ApiProvider throws if nested inside a Redux context', () => {
expect(() =>
render(
<Provider store={configureStore({ reducer: () => null })}>
<ApiProvider api={api}>child</ApiProvider>
</Provider>
)
).toThrowErrorMatchingInlineSnapshot(
'"Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup."'
)
})
})