Skip to content

Commit b1b9207

Browse files
authored
Merge pull request #1576 from Shrugsy/docs/document-currentData-in-hook-results
2 parents cda3258 + 2c1059b commit b1b9207

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.
@@ -228,6 +229,45 @@ function App() {
228229
}
229230
```
230231

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

233273
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)