Skip to content

Commit 1c4a94e

Browse files
authored
docs(useQuery): explain what an identity function is for placeholderData (#6075)
* docs(useQuery): explain identity function for placeholderData clarify the usage of an identity function when used with placeholderData * docs(useQuery): remove the word exported remove the word exported regarding keepPreviousData
1 parent 6e238af commit 1c4a94e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

docs/react/guides/migrating-to-v5.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,16 @@ Since the only supported syntax now is the object syntax, this rule is no longer
234234

235235
We have removed the `keepPreviousData` option and `isPreviousData` flag as they were doing mostly the same thing as `placeholderData` and `isPlaceholderData` flag.
236236

237-
To achieve the same functionality as `keepPreviousData`, we have added previous query `data` as an argument to `placeholderData` function.
238-
Therefore you just need to provide an identity function to `placeholderData` or use `keepPreviousData` function returned from Tanstack Query.
237+
To achieve the same functionality as `keepPreviousData`, we have added previous query `data` as an argument to `placeholderData` which accepts an identity function. Therefore you just need to provide an identity function to `placeholderData` or use the included `keepPreviousData` function from Tanstack Query.
239238

240239
> A note here is that `useQueries` would not receive `previousData` in the `placeholderData` function as argument. This is due to a dynamic nature of queries passed in the array, which may lead to a different shape of result from placeholder and queryFn.
241240
242241
```diff
242+
import {
243+
useQuery,
244+
+ keepPreviousData
245+
} from "@tanstack/react-query";
246+
243247
const {
244248
data,
245249
- isPreviousData,
@@ -252,6 +256,16 @@ const {
252256
});
253257
```
254258

259+
An identity function, in the context of Tanstack Query, refers to a function that always returns its provided argument (i.e. data) unchanged.
260+
261+
```ts
262+
useQuery({
263+
queryKey,
264+
queryFn,
265+
placeholderData: (previousData, previousQuery) => previousData, // identity function with the same behaviour as `keepPreviousData`
266+
})
267+
```
268+
255269
There are some caveats to this change however, which you must be aware of:
256270

257271
- `placeholderData` will always put you into `success` state, while `keepPreviousData` gave you the status of the previous query. That status could be `error` if we have data fetched successfully and then got a background refetch error. However, the error itself was not shared, so we decided to stick with behavior of `placeholderData`.

0 commit comments

Comments
 (0)