|
1 | 1 | import React from 'react' |
2 | | -import { DEFAULT_CONFIG, defaultConfigRef } from '../core/config' |
3 | | -import { ReactQueryConfig } from '../core/types' |
| 2 | + |
| 3 | +import { |
| 4 | + mergeReactQueryConfigs, |
| 5 | + getDefaultedQueryConfig, |
| 6 | + getDefaultedMutationConfig, |
| 7 | +} from '../core/config' |
| 8 | +import { ReactQueryConfig, QueryConfig, MutationConfig } from '../core/types' |
4 | 9 | import { useQueryCache } from './ReactQueryCacheProvider' |
5 | 10 |
|
6 | 11 | const configContext = React.createContext<ReactQueryConfig | undefined>( |
7 | 12 | undefined |
8 | 13 | ) |
9 | 14 |
|
10 | | -export function useConfigContext() { |
11 | | - const queryCache = useQueryCache() |
12 | | - return ( |
13 | | - React.useContext(configContext) || |
14 | | - queryCache.getDefaultConfig() || |
15 | | - defaultConfigRef.current |
16 | | - ) |
| 15 | +function useContextConfig() { |
| 16 | + return React.useContext(configContext) |
| 17 | +} |
| 18 | + |
| 19 | +export function useDefaultedQueryConfig<TResult, TError>( |
| 20 | + config?: QueryConfig<TResult, TError> |
| 21 | +): QueryConfig<TResult, TError> { |
| 22 | + const contextCache = useQueryCache() |
| 23 | + const cache = config?.queryCache || contextCache |
| 24 | + const cacheConfig = cache.getDefaultConfig() |
| 25 | + const contextConfig = useContextConfig() |
| 26 | + return getDefaultedQueryConfig(cacheConfig, contextConfig, { |
| 27 | + ...config, |
| 28 | + queryCache: cache, |
| 29 | + }) |
17 | 30 | } |
18 | 31 |
|
19 | | -export interface ReactQueryProviderConfig extends ReactQueryConfig {} |
| 32 | +export function useDefaultedMutationConfig< |
| 33 | + TResult, |
| 34 | + TError, |
| 35 | + TVariables, |
| 36 | + TSnapshot |
| 37 | +>( |
| 38 | + config?: MutationConfig<TResult, TError, TVariables, TSnapshot> |
| 39 | +): MutationConfig<TResult, TError, TVariables, TSnapshot> { |
| 40 | + const contextCache = useQueryCache() |
| 41 | + const cache = config?.queryCache || contextCache |
| 42 | + const cacheConfig = cache.getDefaultConfig() |
| 43 | + const contextConfig = useContextConfig() |
| 44 | + return getDefaultedMutationConfig(cacheConfig, contextConfig, { |
| 45 | + ...config, |
| 46 | + queryCache: cache, |
| 47 | + }) |
| 48 | +} |
20 | 49 |
|
21 | 50 | export interface ReactQueryConfigProviderProps { |
22 | | - config: ReactQueryProviderConfig |
| 51 | + config: ReactQueryConfig |
23 | 52 | } |
24 | 53 |
|
25 | 54 | export const ReactQueryConfigProvider: React.FC<ReactQueryConfigProviderProps> = ({ |
26 | 55 | config, |
27 | 56 | children, |
28 | 57 | }) => { |
29 | | - const configContextValueOrDefault = useConfigContext() |
30 | | - const configContextValue = React.useContext(configContext) |
31 | | - |
32 | | - const newConfig = React.useMemo<ReactQueryConfig>(() => { |
33 | | - const { shared = {}, queries = {}, mutations = {} } = config |
34 | | - const { |
35 | | - shared: contextShared = {}, |
36 | | - queries: contextQueries = {}, |
37 | | - mutations: contextMutations = {}, |
38 | | - } = configContextValueOrDefault |
39 | | - |
40 | | - return { |
41 | | - shared: { |
42 | | - ...contextShared, |
43 | | - ...shared, |
44 | | - }, |
45 | | - queries: { |
46 | | - ...contextQueries, |
47 | | - ...queries, |
48 | | - }, |
49 | | - mutations: { |
50 | | - ...contextMutations, |
51 | | - ...mutations, |
52 | | - }, |
53 | | - } |
54 | | - }, [config, configContextValueOrDefault]) |
| 58 | + const contextConfig = useContextConfig() |
55 | 59 |
|
56 | | - React.useEffect(() => { |
57 | | - // restore previous config on unmount |
58 | | - return () => { |
59 | | - defaultConfigRef.current = { |
60 | | - ...(configContextValueOrDefault || DEFAULT_CONFIG), |
61 | | - } |
62 | | - } |
63 | | - }, [configContextValueOrDefault]) |
64 | | - |
65 | | - // If this is the outermost provider, overwrite the shared default config |
66 | | - if (!configContextValue) { |
67 | | - defaultConfigRef.current = newConfig |
68 | | - } |
| 60 | + const combinedConfig = React.useMemo( |
| 61 | + () => |
| 62 | + contextConfig ? mergeReactQueryConfigs(contextConfig, config) : config, |
| 63 | + [config, contextConfig] |
| 64 | + ) |
69 | 65 |
|
70 | 66 | return ( |
71 | | - <configContext.Provider value={newConfig}> |
| 67 | + <configContext.Provider value={combinedConfig}> |
72 | 68 | {children} |
73 | 69 | </configContext.Provider> |
74 | 70 | ) |
|
0 commit comments