forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactQueryConfigProvider.js
More file actions
55 lines (46 loc) · 1.25 KB
/
ReactQueryConfigProvider.js
File metadata and controls
55 lines (46 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react'
import { DEFAULT_CONFIG, defaultConfigRef } from '../core/config'
//
const configContext = React.createContext()
export function useConfigContext() {
return React.useContext(configContext) || defaultConfigRef.current
}
export function ReactQueryConfigProvider({ config, children }) {
let configContextValue = useConfigContext()
const newConfig = React.useMemo(() => {
const { shared = {}, queries = {}, mutations = {} } = config
const {
shared: contextShared = {},
queries: contextQueries = {},
mutations: contextMutations = {},
} = configContextValue
return {
shared: {
...contextShared,
...shared,
},
queries: {
...contextQueries,
...queries,
},
mutations: {
...contextMutations,
...mutations,
},
}
}, [config, configContextValue])
React.useEffect(() => {
// restore previous config on unmount
return () => {
defaultConfigRef.current = { ...(configContextValue || DEFAULT_CONFIG) }
}
}, [configContextValue])
if (!configContextValue) {
defaultConfigRef.current = newConfig
}
return (
<configContext.Provider value={newConfig}>
{children}
</configContext.Provider>
)
}