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
Per [the description in `1.9.0-alpha.0`](https://github.com/reduxjs/redux-toolkit/releases/tag/v1.9.0-alpha.0), we plan to remove the "object" argument from `createReducer` and `createSlice.extraReducers` in the future RTK 2.0 major version. In `1.9.0-alpha.0`, we added a one-shot runtime warning to each of those APIs.
13
+
14
+
To simplify upgrading codebases, we've published a set of codemods that will automatically transform the deprecated "object" syntax into the equivalent "builder" syntax.
15
+
16
+
The codemods package is available on NPM as [**`@reduxjs/rtk-codemods`**](https://www.npmjs.com/package/@reduxjs/rtk-codemods). It currently contains two codemods: `createReducerBuilder` and `createSliceBuilder`.
17
+
18
+
To run the codemods against your codebase, run `npx @reduxjs/rtk-codemods <TRANSFORM NAME> path/of/files/ or/some**/*glob.js`.
Copy file name to clipboardExpand all lines: docs/rtk-query/api/created-api/api-slice-utils.mdx
+47-5
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.
0 commit comments