Skip to content

Commit 2c1059b

Browse files
committed
📝 Document currentData in query hook results
1 parent 43bba18 commit 2c1059b

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ type UseQueryOptions = {
261261
type UseQueryResult<T> = {
262262
// Base query state
263263
originalArgs?: unknown // Arguments passed to the query
264-
data?: T // Returned result if present
264+
data?: T // The latest returned result regardless of hook arg, if present
265+
currentData?: T // The latest returned result for the current hook arg, if present
265266
error?: unknown // Error result if present
266267
requestId?: string // A string generated by RTK Query
267268
endpointName?: string // The name of the given endpoint for the query
@@ -391,7 +392,8 @@ type UseQueryStateOptions = {
391392
type UseQueryStateResult<T> = {
392393
// Base query state
393394
originalArgs?: unknown // Arguments passed to the query
394-
data?: T // Returned result if present
395+
data?: T // The latest returned result regardless of hook arg, if present
396+
currentData?: T // The latest returned result for the current hook arg, if present
395397
error?: unknown // Error result if present
396398
requestId?: string // A string generated by RTK Query
397399
endpointName?: string // The name of the given endpoint for the query
@@ -487,7 +489,8 @@ type UseLazyQueryTrigger = (arg: any) => void
487489
type UseQueryStateResult<T> = {
488490
// Base query state
489491
originalArgs?: unknown // Arguments passed to the query
490-
data?: T // Returned result if present
492+
data?: T // The latest returned result regardless of trigger arg, if present
493+
currentData?: T // The latest returned result for the trigger arg, if present
491494
error?: unknown // Error result if present
492495
requestId?: string // A string generated by RTK Query
493496
endpointName?: string // The name of the given endpoint for the query

docs/rtk-query/usage/queries.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ All `refetch`-related options will override the defaults you may have set in [cr
146146

147147
The query hook returns an object containing properties such as the latest `data` for the query request, as well as status booleans for the current request lifecycle state. Below are some of the most frequently used properties. Refer to [`useQuery`](../api/created-api/hooks.mdx#usequery) for an extensive list of all returned properties.
148148

149-
- `data` - The returned result if present.
149+
- `data` - The latest returned result regardless of hook arg, if present.
150+
- `currentData` - The latest returned result for the current hook arg, if present.
150151
- `error` - The error result if present.
151152
- `isUninitialized` - When true, indicates that the query has not started yet.
152153
- `isLoading` - When true, indicates that the query is currently loading for the first time, and has no data yet. This will be `true` for the first request fired off, but _not_ for subsequent requests.
@@ -224,6 +225,45 @@ function App() {
224225
}
225226
```
226227

228+
While `data` is expected to used in the majority of situations, `currentData` is also provided,
229+
which allows for a further level of granularity. For example, if you wanted to show data in the UI
230+
as translucent to represent a re-fetching state, you can use `data` in combination with `isFetching`
231+
to achieve this. However, if you also wish to _only_ show data corresponding to the current arg,
232+
you can instead use `currentData` to achieve this.
233+
234+
In the example below, if posts are being fetched for the first time, a loading skeleton will be
235+
shown. If posts for the current user have previously been fetched, and are re-fetching (e.g. as a
236+
result of a mutation), the UI will show the previous data, but will grey out the data. If the user
237+
changes, it will instead show the skeleton again as opposed to greying out data for the previous user.
238+
239+
```tsx title="Managing UI behavior with currentData"
240+
import { Skeleton } from './Skeleton'
241+
import { useGetPostsByUserQuery } from './api'
242+
243+
function PostsList({ userName }: { userName: string }) {
244+
const { currentData, isFetching, isError } = useGetPostsByUserQuery(userName)
245+
246+
if (isError) return <div>An error has occurred!</div>
247+
248+
if (isFetching && !currentData) return <Skeleton />
249+
250+
return (
251+
<div className={isFetching ? 'posts--disabled' : ''}>
252+
{currentData
253+
? currentData.map((post) => (
254+
<Post
255+
key={post.id}
256+
id={post.id}
257+
name={post.name}
258+
disabled={isFetching}
259+
/>
260+
))
261+
: 'No data available'}
262+
</div>
263+
)
264+
}
265+
```
266+
227267
### Query Cache Keys
228268

229269
When you perform a query, RTK Query automatically serializes the request parameters and creates an internal `queryCacheKey` for the request. Any future request that produces the same `queryCacheKey` will be de-duped against the original, and will share updates if a `refetch` is trigged on the query from any subscribed component.

0 commit comments

Comments
 (0)