Skip to content

Commit e0a8bd5

Browse files
authored
Merge pull request #2804 from reduxjs/docs/v1.9-docs
2 parents 82cf232 + 6763f7b commit e0a8bd5

21 files changed

+2771
-1202
lines changed

.yarn/releases/yarn-3.1.0.cjs

-768
This file was deleted.

.yarn/releases/yarn-3.2.4.cjs

+801
Large diffs are not rendered by default.

.yarnrc.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ nodeLinker: node-modules
22

33
plugins:
44
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
5-
spec: "@yarnpkg/plugin-workspace-tools"
5+
spec: '@yarnpkg/plugin-workspace-tools'
66
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
7-
spec: "@yarnpkg/plugin-version"
7+
spec: '@yarnpkg/plugin-version'
88

9-
yarnPath: .yarn/releases/yarn-3.1.0.cjs
9+
yarnPath: .yarn/releases/yarn-3.2.4.cjs

docs/api/codemods.mdx

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
id: codemods
3+
title: Codemods
4+
sidebar_label: Codemods
5+
hide_title: true
6+
---
7+
8+
 
9+
10+
# Codemods
11+
12+
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`.
19+
20+
Examples:
21+
22+
```bash
23+
npx @reduxjs/rtk-codemods createReducerBuilder ./src
24+
25+
npx @reduxjs/rtk-codemods createSliceBuilder ./packages/my-app/**/*.ts
26+
```
27+
28+
We also recommend re-running Prettier on the codebase before committing the changes.
29+
30+
**These codemods _should_ work, but we would greatly appreciate testing and feedback on more real-world codebases!**
31+
32+
Before:
33+
34+
```js
35+
createReducer(initialState, {
36+
[todoAdded1a]: (state, action) => {
37+
// stuff
38+
},
39+
[todoAdded1b]: (state, action) => action.payload,
40+
})
41+
42+
const slice1 = createSlice({
43+
name: 'a',
44+
initialState: {},
45+
extraReducers: {
46+
[todoAdded1a]: (state, action) => {
47+
// stuff
48+
},
49+
[todoAdded1b]: (state, action) => action.payload,
50+
},
51+
})
52+
```
53+
54+
After:
55+
56+
```js
57+
createReducer(initialState, (builder) => {
58+
builder.addCase(todoAdded1a, (state, action) => {
59+
// stuff
60+
})
61+
62+
builder.addCase(todoAdded1b, (state, action) => action.payload)
63+
})
64+
65+
const slice1 = createSlice({
66+
name: 'a',
67+
initialState: {},
68+
69+
extraReducers: (builder) => {
70+
builder.addCase(todoAdded1a, (state, action) => {
71+
// stuff
72+
})
73+
74+
builder.addCase(todoAdded1b, (state, action) => action.payload)
75+
},
76+
})
77+
```

docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"devDependencies": {
44
"@manaflair/redux-batch": "^1.0.0",
55
"@types/nanoid": "^2.1.0",
6-
"@types/react": "^16.9.46",
6+
"@types/react": "^18.0",
77
"@types/redux-logger": "^3.0.8",
88
"async-mutex": "^0.3.2",
99
"axios": "^0.20.0",

docs/rtk-query/api/createApi.mdx

+43-1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ If you specify `track: false` when manually dispatching queries, RTK Query will
380380

381381
_(required if no `queryFn` provided)_
382382

383+
```ts title="query signature" no-transpile
384+
export type query = <QueryArg>(
385+
arg: QueryArg
386+
) => string | Record<string, unknown>
387+
388+
// with `fetchBaseQuery`
389+
export type query = <QueryArg>(arg: QueryArg) => string | FetchArgs
390+
```
391+
383392
[summary](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
384393
385394
[examples](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
@@ -490,12 +499,45 @@ See also [Invalidating cache data](../usage/automated-refetching.mdx#invalidatin
490499

491500
_(optional, only for query endpoints)_
492501

493-
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.
502+
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.a
494503

495504
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
496505

497506
[examples](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
498507

508+
### `serializeQueryArgs`
509+
510+
_(optional, only for query endpoints)_
511+
512+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.serializeQueryArgs)
513+
514+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.serializeQueryArgs)
515+
516+
### `merge`
517+
518+
_(optional, only for query endpoints)_
519+
520+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
521+
522+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
523+
524+
### `forceRefetch`
525+
526+
_(optional, only for query endpoints)_
527+
528+
```ts title="forceRefetch signature" no-transpile
529+
type forceRefetch = (params: {
530+
currentArg: QueryArg | undefined
531+
previousArg: QueryArg | undefined
532+
state: RootState<any, any, string>
533+
endpointState?: QuerySubState<any>
534+
}) => boolean
535+
```
536+
537+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.forceRefetch)
538+
539+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.forceRefetch)
540+
499541
### `onQueryStarted`
500542
501543
_(optional)_

docs/rtk-query/api/created-api/api-slice-utils.mdx

+47-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ The API slice object includes various utilities that can be used for cache manag
1313
such as implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates),
1414
as well implementing [server side rendering](../../usage/server-side-rendering.mdx).
1515

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+
:::
1723

1824
### `updateQueryData`
1925

@@ -48,8 +54,7 @@ The thunk returns an object containing `{patches: Patch[], inversePatches: Patch
4854

4955
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.
5056

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.
5358

5459
#### Example 1
5560

@@ -105,6 +110,43 @@ dispatch(api.endpoints.getPostById.initiate(1))
105110
dispatch(api.endpoints.getPostById.initiate(1, { ...options }))
106111
```
107112

113+
### `upsertQueryData`
114+
115+
#### Signature
116+
117+
```ts no-transpile
118+
const upsertQueryData = <T>(
119+
endpointName: string,
120+
args: any,
121+
newEntryData: T
122+
) => ThunkAction<Promise<CacheEntry<T>>, PartialState, any, AnyAction>;
123+
```
124+
125+
- **Parameters**
126+
- `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.
141+
142+
#### Example
143+
144+
```ts no-transpile
145+
await dispatch(
146+
api.util.upsertQueryData('getPost', { id: 1 }, { id: 1, text: 'Hello!' })
147+
)
148+
```
149+
108150
### `patchQueryData`
109151

110152
#### Signature
@@ -124,9 +166,9 @@ const patchQueryData = (
124166

125167
#### Description
126168

127-
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.
128170

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`.
130172

131173
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.
132174

0 commit comments

Comments
 (0)