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

keepPreviousData.md

46 lines990 Bytes

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