Open History to see the last commit
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```