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
+43-6Lines changed: 43 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ const api = createApi({
77
77
78
78
### Performing Queries with React Hooks
79
79
80
-
If you're using React Hooks, RTK Query does a few additional things for you. The primary benefit is that you get a render-optimized hook that allows you to have 'background fetching' as well as [derived booleans](#query-hook-return-types) for convenience.
80
+
If you're using React Hooks, RTK Query does a few additional things for you. The primary benefit is that you get a render-optimized hook that allows you to have 'background fetching' as well as [derived booleans](#frequently-used-query-hook-return-values) for convenience.
81
81
82
82
Hooks are automatically generated based on the name of the `endpoint` in the service definition. An endpoint field with `getPost: builder.query()` will generate a hook named `useGetPostQuery`.
83
83
@@ -152,6 +152,47 @@ The way that this component is setup would have some nice traits:
152
152
2. When the request is re-triggered by the polling interval, it will add '...refetching' to the post name
153
153
3. If a user closed this `PostDetail`, but then re-opened it within [the allowed time](../api/createApi#keepunuseddatafor), they would immediately be served a cached result and polling would resume with the previous behavior.
154
154
155
+
### Query Loading State
156
+
157
+
The auto-generated React hooks created by the React-specific version of `createApi` provide [derived booleans](#frequently-used-query-hook-return-values) that reflect the current state of a given query. Derived booleans are preferred for the generated React hooks as opposed to a `status` flag, as the derived booleans are able to provide a greater amount of detail which would not be possible with a single `status` flag, as multiple statuses may be true at a given time (such as `isFetching` and `isSuccess`).
158
+
159
+
For query endpoints, RTK Query maintains a semantic distinction between `isLoading` and `isFetching` in order to provide more flexibility with the derived information provided.
160
+
161
+
-`isLoading` refers to a query being in flight for the _first time_ for the given endpoint + query param combination. No data will be available at this time.
162
+
-`isFetching` refers to a query being in flight for the given endpoint + query param combination, but not necessarily for the first time. Data may be available from an earlier request at this time.
163
+
164
+
This distinction allows for greater control when handling UI behavior. For example, `isLoading` can be used to display a skeleton while loading for the first time, while `isFetching` can be used to grey out old data when fetching subsequent requests when data is invalidated and re-fetched.
165
+
166
+
```tsx title="Managing UI behavior with Query Loading States"
167
+
import { Skeleton } from'./Skeleton'
168
+
import { useGetPostsQuery } from'./api'
169
+
170
+
function App() {
171
+
const { data = [], isLoading, isFetching, isError } =useGetPostsQuery()
172
+
173
+
if (isError) return <div>An error has occurred!</div>
174
+
175
+
if (isLoading) return <Skeleton />
176
+
177
+
return (
178
+
<divclassName={isFetching?'posts--disabled':''}>
179
+
{data.map((post) => (
180
+
<Post
181
+
key={post.id}
182
+
id={post.id}
183
+
name={post.name}
184
+
disabled={isFetching}
185
+
/>
186
+
))}
187
+
</div>
188
+
)
189
+
}
190
+
```
191
+
192
+
### Query Cache Keys
193
+
194
+
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.
195
+
155
196
### Selecting data from a query result
156
197
157
198
Sometimes you may have a parent component that is subscribed to a query, and then in a child component you want to pick an item from that query. In most cases you don't want to perform an additional request for a `getItemById`-type query when you know that you already have the result.
@@ -183,10 +224,6 @@ function PostById({ id }: { id: number }) {
183
224
}
184
225
```
185
226
186
-
### Query Cache Keys
187
-
188
-
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.
189
-
190
227
### Avoiding unnecessary requests
191
228
192
229
By default, if you add a component that makes the same query as an existing one, no request will be performed.
@@ -218,7 +255,7 @@ Click the 'Add bulbasaur' button. You'll observe the same behavior described abo
0 commit comments