Skip to content

Commit a5551ee

Browse files
phryneasmarkerikson
authored andcommitted
ApiProvider: correctly register listeners, allow to disable listeners
1 parent 82c0660 commit a5551ee

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { configureStore } from '@reduxjs/toolkit'
2-
import type { Context } from 'react'
2+
import { Context, useEffect } from 'react'
33
import React from 'react'
44
import type { ReactReduxContextValue } from 'react-redux'
55
import { Provider } from 'react-redux'
@@ -33,7 +33,7 @@ import type { Api } from '@reduxjs/toolkit/dist/query/apiTypes'
3333
export function ApiProvider<A extends Api<any, {}, any, any>>(props: {
3434
children: any
3535
api: A
36-
setupListeners?: Parameters<typeof setupListeners>[1]
36+
setupListeners?: Parameters<typeof setupListeners>[1] | false
3737
context?: Context<ReactReduxContextValue>
3838
}) {
3939
const [store] = React.useState(() =>
@@ -45,7 +45,13 @@ export function ApiProvider<A extends Api<any, {}, any, any>>(props: {
4545
})
4646
)
4747
// Adds the event listeners for online/offline/focus/etc
48-
setupListeners(store.dispatch, props.setupListeners)
48+
useEffect(
49+
(): undefined | (() => void) =>
50+
props.setupListeners === false
51+
? undefined
52+
: setupListeners(store.dispatch, props.setupListeners),
53+
[props.setupListeners]
54+
)
4955

5056
return (
5157
<Provider store={store} context={props.context}>

0 commit comments

Comments
 (0)