Skip to content

Commit 35055d7

Browse files
prateek3255TkDodo
andauthored
feat: Update isLoading behaviour (#4888)
* ♻️ Add isPending and update isLoading behaviour * βœ… Fix useQuery tests * βœ… Fix query core tests * βœ… Fix react-query tests * ♻️ Update mutation to use isPending * ♻️ Mark isInitialLoading as deprecated * βœ… Fix query core mutation tests * βœ… Fix persist query tests * βœ… Fix solid query tests * βœ… Fix all vue query tests * πŸ“ isLoading -> isPending wherever necessary * πŸ“ Use `pending` for `loading` and remove references for `isInitialLoading` * πŸ“ Use `pending` for `loading` and remove references for `isInitialLoading` * πŸ“ Update upgrade guide * πŸ‘Œ Review updates * ✨ Updated examples to use isPending * Update docs/react/reference/useQuery.md --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 5a156e2 commit 35055d7

File tree

82 files changed

+488
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+488
-449
lines changed

β€Ždocs/react/adapters/react-query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Example() {
1616

1717
return (
1818
<div>
19-
{query.isLoading
19+
{query.isPending
2020
? 'Loading...'
2121
: query.isError
2222
? 'Error!'

β€Ždocs/react/adapters/solid-query.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Example() {
2121
return (
2222
<div>
2323
<Switch>
24-
<Match when={query.isLoading}>
24+
<Match when={query.isPending}>
2525
<p>Loading...</p>
2626
</Match>
2727
<Match when={query.isError}>
@@ -126,7 +126,7 @@ export default function App() {
126126

127127
function Example() {
128128
// ❌ react version -- supports destructing outside reactive context
129-
// const { isLoading, error, data } = useQuery({
129+
// const { isPending, error, data } = useQuery({
130130
// queryKey: ['repoData'], () =>
131131
// queryFn: fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
132132
// res.json()
@@ -145,7 +145,7 @@ function Example() {
145145
// βœ… access query properties in JSX reactive context
146146
return (
147147
<Switch>
148-
<Match when={query.isLoading}>Loading...</Match>
148+
<Match when={query.isPending}>Loading...</Match>
149149
<Match when={query.isError}>Error: {query.error.message}</Match>
150150
<Match when={query.isSuccess}>
151151
<div>
@@ -190,7 +190,7 @@ function Example() {
190190
return (
191191
<div>
192192
<Switch>
193-
<Match when={query.isLoading}>
193+
<Match when={query.isPending}>
194194
<p>Loading...</p>
195195
</Match>
196196
<Match when={query.isError}>

β€Ždocs/react/adapters/vue-query.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { useQueryClient, useQuery, useMutation } from "@tanstack/vue-query";
2020
const queryClient = useQueryClient();
2121
2222
// Query
23-
const { isLoading, isError, data, error } = useQuery({ queryKey: ['todos'], queryFn: getTodos });
23+
const { isPending, isError, data, error } = useQuery({ queryKey: ['todos'], queryFn: getTodos });
2424
2525
// Mutation
2626
const mutation = useMutation({
@@ -40,7 +40,7 @@ function onButtonClick() {
4040
</script>
4141
4242
<template>
43-
<span v-if="isLoading">Loading...</span>
43+
<span v-if="isPending">Loading...</span>
4444
<span v-else-if="isError">Error: {{ error.message }}</span>
4545
<!-- We can assume by this point that `isSuccess === true` -->
4646
<ul v-else>

β€Ždocs/react/community/tkdodos-blog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ TanStack Query maintainer [TkDodo](https://twitter.com/tkdodo) has a series of b
1919
2020
## [#4: Status Checks in React Query](https://tkdodo.eu/blog/status-checks-in-react-query)
2121

22-
> We usually check for `isLoading` first before checking for `isError` , but sometimes, checking if `data` is available should be the first thing to do. This article shows how the wrong status check order can negatively impact user experience. [Read more...](https://tkdodo.eu/blog/status-checks-in-react-query)
22+
> We usually check for `isPending` first before checking for `isError` , but sometimes, checking if `data` is available should be the first thing to do. This article shows how the wrong status check order can negatively impact user experience. [Read more...](https://tkdodo.eu/blog/status-checks-in-react-query)
2323
2424
## [#5: Testing React Query](https://tkdodo.eu/blog/testing-react-query)
2525

β€Ždocs/react/guides/background-fetching-indicators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: background-fetching-indicators
33
title: Background Fetching Indicators
44
---
55

6-
A query's `status === 'loading'` state is sufficient enough to show the initial hard-loading state for a query, but sometimes you may want to display an additional indicator that a query is refetching in the background. To do this, queries also supply you with an `isFetching` boolean that you can use to show that it's in a fetching state, regardless of the state of the `status` variable:
6+
A query's `status === 'pending'` state is sufficient enough to show the initial hard-loading state for a query, but sometimes you may want to display an additional indicator that a query is refetching in the background. To do this, queries also supply you with an `isFetching` boolean that you can use to show that it's in a fetching state, regardless of the state of the `status` variable:
77

88
[//]: # 'Example'
99

@@ -19,7 +19,7 @@ function Todos() {
1919
queryFn: fetchTodos,
2020
})
2121

22-
return status === 'loading' ? (
22+
return status === 'pending' ? (
2323
<span>Loading...</span>
2424
) : status === 'error' ? (
2525
<span>Error: {error.message}</span>

β€Ždocs/react/guides/caching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Let's assume we are using the default `gcTime` of **5 minutes** and the default
2323
- A second instance of `useQuery({ queryKey: ['todos'], queryFn: fetchTodos })` mounts elsewhere.
2424
- Since the cache already has data for the `['todos']` key from the first query, that data is immediately returned from the cache.
2525
- The new instance triggers a new network request using its query function.
26-
- Note that regardless of whether both `fetchTodos` query functions are identical or not, both queries' [`status`](../reference/useQuery) are updated (including `isFetching`, `isLoading`, and other related values) because they have the same query key.
26+
- Note that regardless of whether both `fetchTodos` query functions are identical or not, both queries' [`status`](../reference/useQuery) are updated (including `isFetching`, `isPending`, and other related values) because they have the same query key.
2727
- When the request completes successfully, the cache's data under the `['todos']` key is updated with the new data, and both instances are updated with the new data.
2828
- Both instances of the `useQuery({ queryKey: ['todos'], queryFn: fetchTodos })` query are unmounted and no longer in use.
2929
- Since there are no more active instances of this query, a garbage collection timeout is set using `gcTime` to delete and garbage collect the query (defaults to **5 minutes**).

β€Ždocs/react/guides/dependent-queries.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,23 @@ const {
3434
The `projects` query will start in:
3535

3636
```tsx
37-
status: 'loading'
37+
status: 'pending'
38+
isLoading: false
3839
fetchStatus: 'idle'
3940
```
4041

4142
As soon as the `user` is available, the `projects` query will be `enabled` and will then transition to:
4243

4344
```tsx
44-
status: 'loading'
45+
status: 'pending'
46+
isLoading: true
4547
fetchStatus: 'fetching'
4648
```
4749

4850
Once we have the projects, it will go to:
4951

5052
```tsx
5153
status: 'success'
54+
isLoading: false
5255
fetchStatus: 'idle'
5356
```

β€Ždocs/react/guides/disabling-queries.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If you ever want to disable a query from automatically running, you can use the
88
When `enabled` is `false`:
99

1010
- If the query has cached data, then the query will be initialized in the `status === 'success'` or `isSuccess` state.
11-
- If the query does not have cached data, then the query will start in the `status === 'loading'` and `fetchStatus === 'idle'` state.
11+
- If the query does not have cached data, then the query will start in the `status === 'pending'` and `fetchStatus === 'idle'` state.
1212
- The query will not automatically fetch on mount.
1313
- The query will not automatically refetch in the background.
1414
- The query will ignore query client `invalidateQueries` and `refetchQueries` calls that would normally result in the query refetching.
@@ -18,7 +18,7 @@ When `enabled` is `false`:
1818

1919
```tsx
2020
function Todos() {
21-
const { isInitialLoading, isError, data, error, refetch, isFetching } =
21+
const { isLoading, isError, data, error, refetch, isFetching } =
2222
useQuery({
2323
queryKey: ['todos'],
2424
queryFn: fetchTodoList,
@@ -39,7 +39,7 @@ function Todos() {
3939
</>
4040
) : isError ? (
4141
<span>Error: {error.message}</span>
42-
) : isInitialLoading ? (
42+
) : isLoading ? (
4343
<span>Loading...</span>
4444
) : (
4545
<span>Not ready ...</span>
@@ -84,12 +84,12 @@ function Todos() {
8484

8585
[//]: # 'Example2'
8686

87-
### isInitialLoading
87+
### isLoading (Previously: `isInitialLoading`)
8888

89-
Lazy queries will be in `status: 'loading'` right from the start because `loading` means that there is no data yet. This is technically true, however, since we are not currently fetching any data (as the query is not _enabled_), it also means you likely cannot use this flag to show a loading spinner.
89+
Lazy queries will be in `status: 'pending'` right from the start because `pending` means that there is no data yet. This is technically true, however, since we are not currently fetching any data (as the query is not _enabled_), it also means you likely cannot use this flag to show a loading spinner.
9090

91-
If you are using disabled or lazy queries, you can use the `isInitialLoading` flag instead. It's a derived flag that is computed from:
91+
If you are using disabled or lazy queries, you can use the `isLoading` flag instead. It's a derived flag that is computed from:
9292

93-
`isLoading && isFetching`
93+
`isPending && isFetching`
9494

9595
so it will only be true if the query is currently fetching for the first time.

β€Ždocs/react/guides/infinite-queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function Projects() {
6666
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
6767
})
6868

69-
return status === 'loading' ? (
69+
return status === 'pending' ? (
7070
<p>Loading...</p>
7171
) : status === 'error' ? (
7272
<p>Error: {error.message}</p>

β€Ždocs/react/guides/migrating-to-v5.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,14 @@ The `Hydrate` component has been renamed to `HydrationBoundary`. The `Hydrate` c
298298
+ </HydrationBoundary>
299299
```
300300

301+
### `status: loading` has been changed to `status: pending` and `isLoading` has been changed to `isPending` and `isInitialLoading` has now been renamed to `isLoading`
302+
303+
The `loading` status has been renamed to `pending`, and similarly the derived `isLoading` flag has been renamed to `isPending`.
304+
305+
For mutations as well the `status` has been changed from `loading` to `pending` and the `isLoading` flag has been changed to `isPending`.
306+
307+
Lastly the a new derived `isLoading` flag has been added to the queries that is implemented as `isPending && isFetching`. This means that `isLoading` and `isInitialLoading` have the same thing, but `isInitialLoading` is deprecated now and will be removed in the next major version.
308+
309+
To understand the reasoning behing this change checkout the [v5 roadmap discussion](https://github.com/TanStack/query/discussions/4252).
310+
301311
[//]: # 'FrameworkBreakingChanges'

0 commit comments

Comments
Β (0)