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

keepPreviousData.md

40 lines824 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:499](https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L499)
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​
28## Returns
29​
30`T` \| `undefined`
31​
32## Example
33​
34```ts
35new QueryObserver(queryClient, {
36 queryKey: ['posts', page],
37 queryFn: () => fetchPosts(page),
38 placeholderData: keepPreviousData,
39})
40```