You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rtk-query/usage/queries.mdx
+41-1Lines changed: 41 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -146,7 +146,8 @@ All `refetch`-related options will override the defaults you may have set in [cr
146
146
147
147
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.
148
148
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.
150
151
-`error` - The error result if present.
151
152
-`isUninitialized` - When true, indicates that the query has not started yet.
152
153
-`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() {
228
229
}
229
230
```
230
231
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 }) {
if (isError) return <div>An error has occurred!</div>
251
+
252
+
if (isFetching&&!currentData) return <Skeleton />
253
+
254
+
return (
255
+
<divclassName={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
+
231
271
### Query Cache Keys
232
272
233
273
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