Skip to content

Commit 2c49b5d

Browse files
fix(ReactQueryConfigProvider): correctly update defaultConfigRef (TanStack#775)
`useConfigContext` will always be populated. Checking the context value before defaultConfig update
1 parent 8413932 commit 2c49b5d

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/react/ReactQueryConfigProvider.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ export function useConfigContext() {
1010
}
1111

1212
export function ReactQueryConfigProvider({ config, children }) {
13-
let configContextValue = useConfigContext()
13+
let configContextValueOrDefault = useConfigContext()
14+
let configContextValue = React.useContext(configContext)
1415

1516
const newConfig = React.useMemo(() => {
1617
const { shared = {}, queries = {}, mutations = {} } = config
1718
const {
1819
shared: contextShared = {},
1920
queries: contextQueries = {},
2021
mutations: contextMutations = {},
21-
} = configContextValue
22+
} = configContextValueOrDefault
2223

2324
return {
2425
shared: {
@@ -34,15 +35,18 @@ export function ReactQueryConfigProvider({ config, children }) {
3435
...mutations,
3536
},
3637
}
37-
}, [config, configContextValue])
38+
}, [config, configContextValueOrDefault])
3839

3940
React.useEffect(() => {
4041
// restore previous config on unmount
4142
return () => {
42-
defaultConfigRef.current = { ...(configContextValue || DEFAULT_CONFIG) }
43+
defaultConfigRef.current = {
44+
...(configContextValueOrDefault || DEFAULT_CONFIG),
45+
}
4346
}
44-
}, [configContextValue])
47+
}, [configContextValueOrDefault])
4548

49+
// If this is the outermost provider, overwrite the shared default config
4650
if (!configContextValue) {
4751
defaultConfigRef.current = newConfig
4852
}

src/react/tests/ReactQueryConfigProvider.js renamed to src/react/tests/ReactQueryConfigProvider.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,49 @@ describe('ReactQueryConfigProvider', () => {
4848
expect(onSuccess).toHaveBeenCalledWith('data')
4949
})
5050

51+
it('should allow overriding the default config from the outermost provider', async () => {
52+
const outerConfig = {
53+
queries: {
54+
queryFn: jest.fn(async () => {
55+
await sleep(10)
56+
return 'outer'
57+
}),
58+
},
59+
}
60+
61+
const innerConfig = {
62+
queries: {
63+
queryFn: jest.fn(async () => {
64+
await sleep(10)
65+
return 'inner'
66+
}),
67+
},
68+
}
69+
70+
function Container() {
71+
return (
72+
<ReactQueryConfigProvider config={outerConfig}>
73+
<ReactQueryConfigProvider config={innerConfig}>
74+
<h1>Placeholder</h1>
75+
</ReactQueryConfigProvider>
76+
</ReactQueryConfigProvider>
77+
)
78+
}
79+
80+
const rendered = render(<Container />)
81+
82+
await waitFor(() => rendered.findByText('Placeholder'))
83+
84+
await queryCache.prefetchQuery('test')
85+
86+
expect(outerConfig.queries.queryFn).toHaveBeenCalledWith('test')
87+
expect(innerConfig.queries.queryFn).not.toHaveBeenCalled()
88+
89+
const data = queryCache.getQueryData('test')
90+
91+
expect(data).toEqual('outer')
92+
})
93+
5194
it('should reset to defaults when unmounted', async () => {
5295
const onSuccess = jest.fn()
5396

0 commit comments

Comments
 (0)