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
feat(queryObserver): add isInitialLoading derived flag (#4244)
* feat(queryObserver): add isInitialLoading derived flag
this flag describes isLoading && isFetching and can be used to show a loading spinner for lazy or disabled queries
* chore(ci): fix caching
apparently, v3 has a fix: actions/setup-node#543
* chore(ci): properly fix token issue
as described here: https://stackoverflow.com/questions/52015748/npm-failed-to-replace-env-in-config-npm-token
he extra warn output made the caching fail
* chore: fix bundlewatch glob
* docs: add a section about isInitialLoading to the migration guide
Copy file name to clipboardExpand all lines: docs/guides/disabling-queries.md
+14-4Lines changed: 14 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ When `enabled` is `false`:
19
19
```tsx
20
20
function Todos() {
21
21
const {
22
-
isLoading,
22
+
isInitialLoading,
23
23
isError,
24
24
data,
25
25
error,
@@ -45,10 +45,10 @@ function Todos() {
45
45
isError? (
46
46
<span>Error: {error.message}</span>
47
47
) : (
48
-
(isLoading&&!isFetching)? (
49
-
<span>Not ready ...</span>
48
+
isInitialLoading? (
49
+
<span>Loading...</span>
50
50
) : (
51
-
<span>Loading...</span>
51
+
<span>Not ready ...</span>
52
52
)
53
53
)
54
54
)}
@@ -87,3 +87,13 @@ function Todos() {
87
87
)
88
88
}
89
89
```
90
+
91
+
### isInitialLoading
92
+
93
+
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.
94
+
95
+
If you are using disabled or lazy queries, you can use the `isInitialLoading` flag instead. It's a derived flag that is computed from:
96
+
97
+
`isLoading && isFetching`
98
+
99
+
so it will only be true if the query is currently fetching for the first time.
Copy file name to clipboardExpand all lines: docs/guides/migrating-to-react-query-4.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,6 +112,17 @@ This will mostly affect `disabled` queries that don't have any `data` yet, as th
112
112
113
113
Also, have a look at [the guide on dependent queries](../guides/dependent-queries)
114
114
115
+
#### disabled queries
116
+
117
+
Due to this change, disabled queries (even temporarily disabled ones) will start in `loading` state. To make migration easier, especially for having a good flag to know when to display a loading spinner, you can check for `isInitialLoading` instead of `isLoading`:
118
+
119
+
```diff
120
+
- isLoading
121
+
+ isInitialLoading
122
+
```
123
+
124
+
See also the guide on [disabling queries](../guides/disabling-queries#isInitialLoading)
125
+
115
126
### new API for `useQueries`
116
127
117
128
The `useQueries` hook now accepts an object with a `queries` prop as its input. The value of the `queries` prop is an array of queries (this array is identical to what was passed into `useQueries` in v3).
0 commit comments