Skip to content

Commit aaee4c5

Browse files
committed
Remove initialQueries from CacheProvider
1 parent a3ef143 commit aaee4c5

File tree

2 files changed

+1
-198
lines changed

2 files changed

+1
-198
lines changed

src/react/ReactQueryCacheProvider.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,18 @@ import {
33
queryCache as defaultQueryCache,
44
queryCaches,
55
makeQueryCache,
6-
hydrate,
76
} from '../core'
87

98
export const queryCacheContext = React.createContext(defaultQueryCache)
109

1110
export const useQueryCache = () => React.useContext(queryCacheContext)
1211

13-
export function ReactQueryCacheProvider({
14-
queryCache,
15-
initialQueries,
16-
children,
17-
}) {
12+
export function ReactQueryCacheProvider({ queryCache, children }) {
1813
const resolvedQueryCache = React.useMemo(
1914
() => queryCache || makeQueryCache(),
2015
[queryCache]
2116
)
2217

23-
const initializeQueries = React.useMemo(() => {
24-
if (initialQueries) {
25-
return hydrate(resolvedQueryCache, initialQueries)
26-
}
27-
}, [resolvedQueryCache, initialQueries])
28-
29-
React.useEffect(() => {
30-
if (initializeQueries) {
31-
initializeQueries()
32-
}
33-
}, [initializeQueries])
34-
3518
React.useEffect(() => {
3619
queryCaches.push(resolvedQueryCache)
3720

src/react/tests/ReactQueryCacheProvider.test.js

Lines changed: 0 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
useQuery,
88
useQueryCache,
99
queryCaches,
10-
dehydrate,
1110
} from '../index'
1211
import { sleep } from './utils'
1312

@@ -184,183 +183,4 @@ describe('ReactQueryCacheProvider', () => {
184183
expect(caches[0].clear).toHaveBeenCalled()
185184
customCache.clear({ notify: false })
186185
})
187-
188-
describe('hydration', () => {
189-
const fetchData = value =>
190-
new Promise(res => setTimeout(() => res(value), 10))
191-
const dataQuery = key => fetchData(key)
192-
let stringifiedQueries
193-
194-
beforeAll(async () => {
195-
const serverQueryCache = makeQueryCache()
196-
await serverQueryCache.prefetchQuery('string', dataQuery)
197-
const dehydrated = dehydrate(serverQueryCache)
198-
stringifiedQueries = JSON.stringify(dehydrated)
199-
serverQueryCache.clear({ notify: false })
200-
})
201-
202-
test('should hydrate initialQueries to default cache', async () => {
203-
const dehydratedQueries = JSON.parse(stringifiedQueries)
204-
function Page() {
205-
const { data } = useQuery('string', dataQuery)
206-
207-
return (
208-
<div>
209-
<h1>{data}</h1>
210-
</div>
211-
)
212-
}
213-
214-
const rendered = render(
215-
<ReactQueryCacheProvider initialQueries={dehydratedQueries}>
216-
<Page />
217-
</ReactQueryCacheProvider>
218-
)
219-
220-
rendered.getByText('string')
221-
})
222-
223-
test('should hydrate initialQueries to provided cache', async () => {
224-
const dehydratedQueries = JSON.parse(stringifiedQueries)
225-
const clientQueryCache = makeQueryCache()
226-
227-
function Page() {
228-
const { data } = useQuery('string', dataQuery)
229-
return (
230-
<div>
231-
<h1>{data}</h1>
232-
</div>
233-
)
234-
}
235-
236-
const rendered = render(
237-
<ReactQueryCacheProvider
238-
initialQueries={dehydratedQueries}
239-
queryCache={clientQueryCache}
240-
>
241-
<Page />
242-
</ReactQueryCacheProvider>
243-
)
244-
245-
rendered.getByText('string')
246-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(false)
247-
await waitFor(() =>
248-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(true)
249-
)
250-
251-
clientQueryCache.clear({ notify: false })
252-
})
253-
254-
test('should hydrate new queries if initialQueries changes', async () => {
255-
const dehydratedQueries = JSON.parse(stringifiedQueries)
256-
const clientQueryCache = makeQueryCache()
257-
258-
function Page({ queryKey }) {
259-
const { data } = useQuery(queryKey, dataQuery)
260-
return (
261-
<div>
262-
<h1>{data}</h1>
263-
</div>
264-
)
265-
}
266-
267-
const rendered = render(
268-
<ReactQueryCacheProvider
269-
initialQueries={dehydratedQueries}
270-
queryCache={clientQueryCache}
271-
>
272-
<Page queryKey={'string'} />
273-
</ReactQueryCacheProvider>
274-
)
275-
276-
rendered.getByText('string')
277-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(false)
278-
await waitFor(() =>
279-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(true)
280-
)
281-
282-
const intermediateCache = makeQueryCache()
283-
await intermediateCache.prefetchQuery('string', () =>
284-
dataQuery('should not change')
285-
)
286-
await intermediateCache.prefetchQuery('added string', dataQuery)
287-
const dehydrated = dehydrate(intermediateCache)
288-
intermediateCache.clear({ notify: false })
289-
290-
rendered.rerender(
291-
<ReactQueryCacheProvider
292-
initialQueries={dehydrated}
293-
queryCache={clientQueryCache}
294-
>
295-
<Page queryKey={'string'} />
296-
<Page queryKey={'added string'} />
297-
</ReactQueryCacheProvider>
298-
)
299-
300-
// Existing query data should not be overwritten,
301-
// so this should still be the original data
302-
rendered.getByText('string')
303-
// But new query data should be available immediately
304-
rendered.getByText('added string')
305-
expect(clientQueryCache.getQuery('added string').state.isStale).toBe(
306-
false
307-
)
308-
await waitFor(() =>
309-
expect(clientQueryCache.getQuery('added string').state.isStale).toBe(
310-
true
311-
)
312-
)
313-
314-
clientQueryCache.clear({ notify: false })
315-
})
316-
317-
test('should hydrate queries to new cache if cache changes', async () => {
318-
const dehydratedQueries = JSON.parse(stringifiedQueries)
319-
const clientQueryCache = makeQueryCache()
320-
321-
function Page() {
322-
const { data } = useQuery('string', dataQuery)
323-
return (
324-
<div>
325-
<h1>{data}</h1>
326-
</div>
327-
)
328-
}
329-
330-
const rendered = render(
331-
<ReactQueryCacheProvider
332-
initialQueries={dehydratedQueries}
333-
queryCache={clientQueryCache}
334-
>
335-
<Page />
336-
</ReactQueryCacheProvider>
337-
)
338-
339-
rendered.getByText('string')
340-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(false)
341-
await waitFor(() =>
342-
expect(clientQueryCache.getQuery('string').state.isStale).toBe(true)
343-
)
344-
345-
const newClientQueryCache = makeQueryCache()
346-
347-
rendered.rerender(
348-
<ReactQueryCacheProvider
349-
initialQueries={dehydratedQueries}
350-
queryCache={newClientQueryCache}
351-
>
352-
<Page />
353-
</ReactQueryCacheProvider>
354-
)
355-
356-
rendered.getByText('string')
357-
expect(newClientQueryCache.getQuery('string').state.isStale).toBe(false)
358-
await waitFor(() =>
359-
expect(newClientQueryCache.getQuery('string').state.isStale).toBe(true)
360-
)
361-
362-
clientQueryCache.clear({ notify: false })
363-
newClientQueryCache.clear({ notify: false })
364-
})
365-
})
366186
})

0 commit comments

Comments
 (0)