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: remove idle state
in favor of status: loading & fetchStatus: idle
* feat: remove idle state
remove isIdle boolean
* feat: remove idle state
documentation around the removed idle state and the new fetchingStatus
* feat: remove idle state
add missing ' to docs
Permanently disabling a query opts out of many great features that react-query has to offer (like background refetches), and it's also not the idiomatic way. It takes you from the declartive approach (defining dependencies when your query should run) into an imperative mode (fetch whenever I click here). It is also not possible to pass parameters to `refetch`. Oftentimes, all you want is a lazy query that defers the initial fetch:
63
+
64
+
## Lazy Queries
65
+
66
+
The enabled option can not only be used to permenantly disable a query, but also to enable / disable it at a later time. A good example would be a filter form where you only want to fire off the first request once the user has entered a filter value:
67
+
68
+
```jsx
69
+
functionTodos() {
70
+
const [filter, setFilter] =React.useState('')
71
+
72
+
const { data } =useQuery(
73
+
['todos', filter],
74
+
() =>fetchTodos(filter),
75
+
{
76
+
// ⬇️ disabled as long as the filter is empty
77
+
enabled:!!filter
78
+
}
79
+
)
80
+
81
+
return (
82
+
<div>
83
+
// 🚀 applying the filter will enable and execute the query
Copy file name to clipboardExpand all lines: docs/src/pages/guides/network-mode.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Since React Query is most often used for data fetching in combination with data
9
9
10
10
## Network Mode: online
11
11
12
-
In this mode, Queries and Mutations will not fire unless you have network connection. This is the default mode. If a fetch is initiated for a query, it will always stay in the `state` (`loading`, `idle`, `error`, `success`) it is in if the fetch cannot be made because there is no network connection. However, a `fetchStatus` is exposed additionally. This can be either:
12
+
In this mode, Queries and Mutations will not fire unless you have network connection. This is the default mode. If a fetch is initiated for a query, it will always stay in the `state` (`loading`, `error`, `success`) it is in if the fetch cannot be made because there is no network connection. However, a [fetchStatus](./queries#fetchstatus) is exposed additionally. This can be either:
13
13
14
14
-`fetching`: The `queryFn` is really executing - a request is in-flight.
15
15
-`paused`: The query is not executing - it is `paused` until you have connection again
@@ -41,6 +41,6 @@ The [React Query Devtools](../devtools) will show Queries in a `paused` state if
Copy file name to clipboardExpand all lines: docs/src/pages/guides/queries.md
+23-3Lines changed: 23 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,16 +32,14 @@ const result = useQuery(['todos'], fetchTodoList)
32
32
33
33
The `result` object contains a few very important states you'll need to be aware of to be productive. A query can only be in one of the following states at any given moment:
34
34
35
-
-`isLoading` or `status === 'loading'` - The query has no data and is currently fetching
35
+
-`isLoading` or `status === 'loading'` - The query has no data yet
36
36
-`isError` or `status === 'error'` - The query encountered an error
37
37
-`isSuccess` or `status === 'success'` - The query was successful and data is available
38
-
-`isIdle` or `status === 'idle'` - The query is currently disabled (you'll learn more about this in a bit)
39
38
40
39
Beyond those primary states, more information is available depending on the state of the query:
41
40
42
41
-`error` - If the query is in an `isError` state, the error is available via the `error` property.
43
42
-`data` - If the query is in a `success` state, the data is available via the `data` property.
44
-
-`isFetching` - In any state, if the query is fetching at any time (including background refetching) `isFetching` will be `true`.
45
43
46
44
For **most** queries, it's usually sufficient to check for the `isLoading` state, then the `isError` state, then finally, assume that the data is available and render the successful state:
47
45
@@ -92,6 +90,28 @@ function Todos() {
92
90
)
93
91
}
94
92
```
93
+
94
+
TypeScript will also narrow the type of `data` correctly if you've checked for `loading` and `error` before accessing it.
95
+
96
+
### FetchStatus
97
+
98
+
In addition to the `status` field, the `result` object, you will also get an additional `fetchStatus`property with the following options:
99
+
100
+
-`fetchStatus === 'fetching'` - The query is currently fetching.
101
+
-`fetchStatus === 'paused'` - The query wanted to fetch, but it is paused. Read more about this in the [Network Mode](./network-mode) guide.
102
+
-`fetchStatus === 'idle'` - The query is not doing anything at the moment.
103
+
104
+
### Why two different states?
105
+
106
+
Background refetches and stale-while-revalidate logic make all combinations for `status` and `fetchStatus` possible. For example:
107
+
- a query in `success` status will usually be in `idle` fetchStatus, but it could also be in `fetching` if a background refetch is happening.
108
+
- a query that mounts and has no data will usually be in `loading` status and `fetching` fetchStatus, but it could also be `paused` if there is no network connection.
109
+
110
+
So keep in mind that a query can be in `loading` state without actually fetching data. As a rule of thumb:
111
+
112
+
- The `status` gives information about the `data`: Do we have any or not?
113
+
- The `fetchStatus` gives information about the `queryFn`: Is it running or not?
114
+
95
115
## Further Reading
96
116
97
117
For an alternative way of performing status checks, have a look at the [Community Resources](../community/tkdodos-blog#4-status-checks-in-react-query).
Copy file name to clipboardExpand all lines: docs/src/pages/reference/useQuery.md
+2-5Lines changed: 2 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,12 +190,9 @@ const result = useQuery({
190
190
191
191
- `status: String`
192
192
- Will be:
193
-
- `idle` if the query is idle. This only happens if a query is initialized with `enabled: false` and no initial data is available.
194
193
- `loading` if the query is in a "hard" loading state. This means there is no cached data and the query is currently fetching, eg `isFetching === true`
195
194
- `error` if the query attempt resulted in an error. The corresponding `error` property has the error received from the attempted fetch
196
195
- `success` if the query has received a response with no errors and is ready to display its data. The corresponding `data` property on the query is the data received from the successful fetch or if the query's `enabled` property is set to `false` and has not been fetched yet `data` is the first `initialData` supplied to the query on initialization.
197
-
- `isIdle: boolean`
198
-
- A derived boolean from the `status` variable above, provided for convenience.
199
196
- `isLoading: boolean`
200
197
- A derived boolean from the `status` variable above, provided for convenience.
201
198
- `isSuccess: boolean`
@@ -229,8 +226,8 @@ const result = useQuery({
229
226
- This property can be used to not show any previously cached data.
230
227
- `fetchStatus: FetchStatus`
231
228
- `fetching`: Is `true` whenever the queryFn is executing, which includes initial `loading` as well as background refetches.
232
-
- `paused`: The query wanted to fetch, but has been `paused`
233
-
- `idle`: The query is not fetching
229
+
- `paused`: The query wanted to fetch, but has been `paused`.
230
+
- `idle`: The query is not fetching.
234
231
- see [Network Mode](../guides/network-mode) for more information.
235
232
- `isFetching: boolean`
236
233
- A derived boolean from the `fetchStatus` variable above, provided for convenience.
0 commit comments