Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

keepPreviousData.md

44 lines917 Bytes

1---
2id: keepPreviousData
3title: keepPreviousData
4---
5​
6```ts
7function keepPreviousData<T>(previousData: T | undefined): T | undefined;
8```
9​
10Defined in: [packages/query-core/src/utils.ts:582](https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L582)
11​
12Intended to be passed as a query's `placeholderData` option, for example
13`placeholderData: keepPreviousData`. Instead of resetting the query's data to `undefined` while a new
14query key is fetching, it keeps displaying the previously fetched data until the new data arrives.
15​
16## Type Parameters
17​
18### T
19​
20`T`
21​
22## Parameters
23​
24### previousData
25​
26`T` \| `undefined`
27​
28The data of the previous query key, passed by the observer.
29​
30## Returns
31​
32`T` \| `undefined`
33​
34The previous data, unchanged.
35​
36## Example
37​
38```ts
39new QueryObserver(queryClient, {
40 queryKey: ['posts', page],
41 queryFn: () => fetchPosts(page),
42 placeholderData: keepPreviousData,
43})
44```