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
+47-5Lines changed: 47 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,13 @@ The API slice object includes various utilities that can be used for cache manag
13
13
such as implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates),
14
14
as well implementing [server side rendering](../../usage/server-side-rendering.mdx).
15
15
16
-
These are included in a `util` field inside the slice object.
16
+
These are included as `api.util` inside the API object.
17
+
18
+
:::info
19
+
20
+
Some of the TS types on this page are pseudocode to illustrate intent, as the actual internal types are fairly complex.
21
+
22
+
:::
17
23
18
24
### `updateQueryData`
19
25
@@ -48,8 +54,7 @@ The thunk returns an object containing `{patches: Patch[], inversePatches: Patch
48
54
49
55
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.
50
56
51
-
Note that the first two arguments (`endpointName` and `args`) are used to determine which existing
52
-
cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
57
+
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.
-`endpointName`: a string matching an existing endpoint name
127
+
-`args`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
128
+
-`newEntryValue`: the value to be written into the corresponding cache entry's `data` field
129
+
130
+
#### Description
131
+
132
+
A Redux thunk action creator that, when dispatched, acts as an artificial API request to upsert a value into the cache.
133
+
134
+
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.
135
+
136
+
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.
137
+
138
+
The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated.
139
+
140
+
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.
A Redux thunk action creator that applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
169
+
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.
128
170
129
-
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a JSON diff/patch array as produced by Immer's `produceWithPatches`.
171
+
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`.
130
172
131
173
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.
* A Redux thunk that can be used to manually trigger pre-fetching of data.
231
231
*
232
-
* The thunk 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.
232
+
* The thunk 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 set of options used to determine if the data actually should be re-fetched based on cache staleness.
233
233
*
234
234
* React Hooks users will most likely never need to use this directly, as the `usePrefetch` hook will dispatch this thunk internally as needed when you call the prefetching function supplied by the hook.
* 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.
249
249
*
250
-
* 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.
250
+
* 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 an `updateRecipe` 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.
251
251
*
252
-
* 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).
252
+
* 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).
253
253
*
254
254
* 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.
255
255
*
256
+
* 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.
* A Redux thunk action creator that, when dispatched, acts as an artificial API request to upsert a value into the cache.
279
+
*
280
+
* 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.
281
+
*
282
+
* 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.
283
+
*
284
+
* The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated.
285
+
*
286
+
* 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.
* A Redux thunk that applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
281
302
*
282
-
* The thunk accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a JSON diff/patch array as produced by Immer's `produceWithPatches`.
303
+
* The thunk 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`.
283
304
*
284
305
* 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.
0 commit comments