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/api/created-api/api-slice-utils.mdx
+25-25Lines changed: 25 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -23,12 +23,14 @@ Some of the TS types on this page are pseudocode to illustrate intent, as the ac
23
23
24
24
### `updateQueryData`
25
25
26
+
A Redux thunk action creator that, when dispatched, creates and applies a set of JSON diff/patch objects to the current state. This immediately updates the Redux state with those changes.
-`endpointName`: a string matching an existing endpoint name
45
-
-`args`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
47
+
-`arg`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
46
48
-`updateRecipe`: an Immer `produce` callback that can apply changes to the cached state
47
49
-`updateProvided`: a boolean indicating whether the endpoint's provided tags should be re-calculated based on the updated cache. Defaults to `false`.
48
50
49
51
#### Description
50
52
51
-
A Redux thunk action creator that, when dispatched, creates and applies a set of JSON diff/patch objects to the current state. This immediately updates the Redux state with those changes.
52
-
53
53
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a callback function. The callback receives an Immer-wrapped `draft` of the current state, and may modify the draft to match the expected results after the mutation completes successfully.
54
54
55
55
The thunk returns an object containing `{patches: Patch[], inversePatches: Patch[], undo: () => void}`. The `patches` and `inversePatches` are generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).
56
56
57
-
This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, args, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
57
+
This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, arg, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
58
58
59
-
Note that the first two arguments (`endpointName` and `args`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
59
+
Note that the first two arguments (`endpointName` and `arg`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
-`endpointName`: a string matching an existing endpoint name
126
-
-`args`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
128
+
-`arg`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
127
129
-`newEntryValue`: the value to be written into the corresponding cache entry's `data` field
128
130
129
131
#### Description
130
132
131
-
A Redux thunk action creator that, when dispatched, acts as an artificial API request to upsert a value into the cache.
132
-
133
133
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), the appropriate query arg values to construct the desired cache key, and the data to upsert.
134
134
135
135
If no cache entry for that cache key exists, a cache entry will be created and the data added. If a cache entry already exists, this will _overwrite_ the existing cache entry data.
136
136
137
-
The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated.
137
+
The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated. This includes executing the `transformResponse` callback if defined for that endpoint.
138
138
139
139
If dispatched while an actual request is in progress, both the upsert and request will be handled as soon as they resolve, resulting in a "last result wins" update behavior.
140
140
@@ -148,27 +148,27 @@ await dispatch(
148
148
149
149
### `patchQueryData`
150
150
151
+
A Redux thunk action creator that, when dispatched, applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
-`endpointName`: a string matching an existing endpoint name
164
-
-`args`: a cache key, used to determine which cached dataset needs to be updated
166
+
-`arg`: a cache key, used to determine which cached dataset needs to be updated
165
167
-`patches`: an array of patches (or inverse patches) to apply to cached state. These would typically be obtained from the result of dispatching [`updateQueryData`](#updatequerydata)
166
168
-`updateProvided`: a boolean indicating whether the endpoint's provided tags should be re-calculated based on the updated cache. Defaults to `false`.
167
169
168
170
#### Description
169
171
170
-
A Redux thunk action creator that, when dispatched, applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
171
-
172
172
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), the appropriate query arg values to construct the desired cache key, and a JSON diff/patch array as produced by Immer's `produceWithPatches`.
173
173
174
174
This is typically used as the second step in implementing optimistic updates. If a request fails, the optimistically-applied changes can be reverted by dispatching `patchQueryData` with the `inversePatches` that were generated by `updateQueryData` earlier.
@@ -279,6 +279,8 @@ const api = createApi({
279
279
280
280
### `prefetch`
281
281
282
+
A Redux thunk action creator that can be used to manually trigger pre-fetching of data.
A Redux thunk action creator that can be used to manually trigger pre-fetching of data.
302
-
303
303
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a set of options used to determine if the data actually should be re-fetched based on cache staleness.
304
304
305
305
React Hooks users will most likely never need to use this directly, as the `usePrefetch` hook will dispatch the thunk action creator result internally as needed when you call the prefetching function supplied by the hook.
* The thunk executes _synchronously_, and returns an object containing `{patches: Patch[], inversePatches: Patch[], undo: () => void}`. The `patches` and `inversePatches` are generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).
239
239
*
240
-
* This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, args, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
240
+
* This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, arg, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
241
241
*
242
-
* Note that the first two arguments (`endpointName` and `args`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
242
+
* Note that the first two arguments (`endpointName` and `arg`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
0 commit comments