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
* Bail out if query data undefined
* Fix failing test
* docs: migration guide for undefined data
* docs: update setQueryData reference
* Update docs/src/pages/guides/migrating-to-react-query-4.md
Co-authored-by: Louis Law <[email protected]>
Co-authored-by: Dominik Dorfmeister <[email protected]>
Copy file name to clipboardExpand all lines: docs/src/pages/guides/migrating-to-react-query-4.md
+26
Original file line number
Diff line number
Diff line change
@@ -245,6 +245,21 @@ Types now require using TypeScript v4.1 or greater
245
245
Starting with v4, react-query will no longer log errors (e.g. failed fetches) to the console in production mode, as this was confusing to many.
246
246
Errors will still show up in development mode.
247
247
248
+
### Undefined is an illegale cache value for successful queries
249
+
250
+
In order to make bailing out of updates possible by returning `undefined`, we had to make `undefined` an illegal cache value. This is in-line with other concepts of react-query, for example, returning `undefined` from the [initialData function](guides/initial-query-data#initial-data-function) will also _not_ set data.
251
+
252
+
Further, it is an easy bug to produce `Promise<void>` by adding logging in the queryFn:
This is now disallowed on type level; at runtime, `undefined` will be transformed to a _failed Promise_, which means you will get an `error`, which will also be logged to the console in development mode.
262
+
248
263
## New Features 🚀
249
264
250
265
### Proper offline support
@@ -265,3 +280,14 @@ Mutations can now also be garbage collected automatically, just like queries. Th
265
280
### Tracked Queries per default
266
281
267
282
React Query defaults to "tracking" query properties, which should give you a nice boost in render optimization. The feature has existed since [v3.6.0](https://github.com/tannerlinsley/react-query/releases/tag/v3.6.0) and has now become the default behavior with v4.
283
+
284
+
### Bailing out of updates with setQueryData
285
+
286
+
When using the [functional updater form of setQueryData](../reference/QueryClient#queryclientsetquerydata), you can now bail out of the update by returning `undefined`. This is helpful if `undefined` is given to you as `previousValue`, which means that currently, no cached entry exists and you don't want to / cannot create one, like in the example of toggling a todo:
If the value is `undefined`, the query data is not updated.
228
+
227
229
**Using an updater function**
228
230
229
231
For convenience in syntax, you can also pass an updater function which receives the current data value and returns the new one:
@@ -232,6 +234,8 @@ For convenience in syntax, you can also pass an updater function which receives
232
234
setQueryData(queryKey, oldData=> newData)
233
235
```
234
236
237
+
If the updater function returns `undefined`, the query data will not be updated. If the updater function receives `undefined` as input, you can return `undefined` to bail out of the update and thus _not_ create a new cache entry.
238
+
235
239
## `queryClient.getQueryState`
236
240
237
241
`getQueryState` is a synchronous function that can be used to get an existing query's state. If the query does not exist, `undefined` will be returned.
Copy file name to clipboardExpand all lines: docs/src/pages/reference/useQuery.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,7 @@ const result = useQuery({
68
68
69
69
**Options**
70
70
71
-
- `queryKey:unknown[]`
71
+
- `queryKey: unknown[]`
72
72
- **Required**
73
73
- The query key to use for this query.
74
74
- The query key will be hashed into a stable hash. See [Query Keys](../guides/query-keys) for more information.
@@ -77,7 +77,7 @@ const result = useQuery({
77
77
- **Required, but only if no default query function has been defined** See [Default Query Function](../guides/default-query-function) for more information.
78
78
- The function that the query will use to request data.
79
79
- Receives a [QueryFunctionContext](../guides/query-functions#queryfunctioncontext)
80
-
- Must return a promise that will either resolve data or throw an error.
80
+
- Must return a promise that will either resolve data or throw an error. The data cannot be `undefined`.
81
81
- `enabled: boolean`
82
82
- Set this to `false` to disable this query from automatically running.
83
83
- Can be used for [Dependent Queries](../guides/dependent-queries).
0 commit comments