Skip to content

Commit 5b3bf2c

Browse files
committed
Remove reducer object notation docs examples
1 parent 57be8ed commit 5b3bf2c

File tree

3 files changed

+30
-95
lines changed

3 files changed

+30
-95
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,14 @@ type RejectedWithValue = <ThunkArg, RejectedValue>(
210210
) => RejectedWithValueAction<ThunkArg, RejectedValue>
211211
```
212212
213-
To handle these actions in your reducers, reference the action creators in `createReducer` or `createSlice` using either the object key notation or the "builder callback" notation. (Note that if you use TypeScript, you [should use the "builder callback" notation to ensure the types are inferred correctly](../usage/usage-with-typescript.md#type-safety-with-extrareducers)):
213+
To handle these actions in your reducers, reference the action creators in `createReducer` or `createSlice` using the "builder callback" notation.
214214
215215
```ts no-transpile {2,6,14,23}
216-
const reducer1 = createReducer(initialState, {
217-
[fetchUserById.fulfilled]: (state, action) => {},
218-
})
219-
220-
const reducer2 = createReducer(initialState, (builder) => {
216+
const reducer1 = createReducer(initialState, (builder) => {
221217
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
222218
})
223219

224-
const reducer3 = createSlice({
225-
name: 'users',
226-
initialState,
227-
reducers: {},
228-
extraReducers: {
229-
[fetchUserById.fulfilled]: (state, action) => {},
230-
},
231-
})
232-
233-
const reducer4 = createSlice({
220+
const reducer2 = createSlice({
234221
name: 'users',
235222
initialState,
236223
reducers: {},
@@ -546,19 +533,20 @@ test('this thunk should always be skipped', async () => {
546533
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
547534
import { userAPI, User } from './userAPI'
548535

549-
const fetchUserById = createAsyncThunk<User, string, {
550-
state: { users: { loading: string, currentRequestId: string } }
551-
}> (
552-
'users/fetchByIdStatus',
553-
async (userId: string, { getState, requestId }) => {
554-
const { currentRequestId, loading } = getState().users
555-
if (loading !== 'pending' || requestId !== currentRequestId) {
556-
return
557-
}
558-
const response = await userAPI.fetchById(userId)
559-
return response.data
536+
const fetchUserById = createAsyncThunk<
537+
User,
538+
string,
539+
{
540+
state: { users: { loading: string; currentRequestId: string } }
560541
}
561-
)
542+
>('users/fetchByIdStatus', async (userId: string, { getState, requestId }) => {
543+
const { currentRequestId, loading } = getState().users
544+
if (loading !== 'pending' || requestId !== currentRequestId) {
545+
return
546+
}
547+
const response = await userAPI.fetchById(userId)
548+
return response.data
549+
})
562550

563551
const usersSlice = createSlice({
564552
name: 'users',

docs/api/createReducer.mdx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ function counterReducer(state = initialState, action) {
3737
This approach works well, but is a bit boilerplate-y and error-prone. For instance, it is easy to forget the `default` case or
3838
setting the initial state.
3939

40-
The `createReducer` helper streamlines the implementation of such reducers. It supports two different forms of defining case
41-
reducers to handle actions: a "builder callback" notation and a "map object" notation. Both are equivalent, but the "builder callback"
42-
notation is preferred.
40+
The `createReducer` helper streamlines the implementation of such reducers. It uses a "builder callback" notation to define handlers for specific action types, matching against a range of actions, or handling a default case. This is conceptually similar to a switch statement, but with better TS support.
4341

4442
With `createReducer`, your reducers instead look like:
4543

@@ -74,8 +72,6 @@ const counterReducer = createReducer(initialState, (builder) => {
7472

7573
[overloadSummary](docblock://createReducer.ts?token=createReducer&overload=0)
7674

77-
The recommended way of using `createReducer` is the builder callback notation, as it works best with TypeScript and most IDEs.
78-
7975
### Parameters
8076

8177
[params](docblock://createReducer.ts?token=createReducer&overload=0)
@@ -110,17 +106,6 @@ The recommended way of using `createReducer` is the builder callback notation, a
110106

111107
[params,examples](docblock://mapBuilders.ts?token=ActionReducerMapBuilder.addDefaultCase)
112108

113-
## Usage with the "Map Object" Notation
114-
115-
[overloadSummary](docblock://createReducer.ts?token=createReducer&overload=1)
116-
117-
While this notation is a bit shorter, it works only in JavaScript, not TypeScript and has less integration with IDEs,
118-
so we recommend the "builder callback" notation in most cases.
119-
120-
### Parameters
121-
122-
[params](docblock://createReducer.ts?token=createReducer&overload=1)
123-
124109
### Returns
125110

126111
The generated reducer function.

docs/api/createSlice.mdx

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ function createSlice({
5959
initialState: any,
6060
// An object of "case reducers". Key names will be used to generate actions.
6161
reducers: Object<string, ReducerFunction | ReducerAndPrepareObject>
62-
// A "builder callback" function used to add more reducers, or
63-
// an additional object of "case reducers", where the keys should be other
64-
// action types
65-
extraReducers?:
66-
| Object<string, ReducerFunction>
67-
| ((builder: ActionReducerMapBuilder<State>) => void)
62+
// A "builder callback" function used to add more reducers
63+
extraReducers?: ((builder: ActionReducerMapBuilder<State>) => void)
6864
})
6965
```
7066

@@ -149,48 +145,12 @@ the function from `reducers` will be used to handle that action type.
149145

150146
### The `extraReducers` "builder callback" notation
151147

152-
The recommended way of using `extraReducers` is to use a callback that receives a `ActionReducerMapBuilder` instance.
153-
154-
This builder notation is also the only way to add matcher reducers and default case reducers to your slice.
148+
Similar to `createReducer`, the `extraReducers` field uses a "builder callback" notation to define handlers for specific action types, matching against a range of actions, or handling a default case. This is conceptually similar to a switch statement, but with better TS support as it can infer the action type from the provided action creator. It's particularly useful for working with actions produced by `createAction` and `createAsyncThunk`.
155149

156150
[examples](docblock://createSlice.ts?token=CreateSliceOptions.extraReducers)
157151

158-
We recommend using this API as it has better TypeScript support (and thus, IDE autocomplete even for JavaScript users), as it will correctly infer the action type in the reducer based on the provided action creator.
159-
It's particularly useful for working with actions produced by `createAction` and `createAsyncThunk`.
160-
161152
See [the "Builder Callback Notation" section of the `createReducer` reference](./createReducer.mdx#usage-with-the-builder-callback-notation) for details on how to use `builder.addCase`, `builder.addMatcher`, and `builder.addDefault`
162153

163-
### The `extraReducers` "map object" notation
164-
165-
Like `reducers`, `extraReducers` can be an object containing Redux case reducer functions. However, the keys should
166-
be other Redux string action type constants, and `createSlice` will _not_ auto-generate action types or action creators
167-
for reducers included in this parameter.
168-
169-
Action creators that were generated using [`createAction`](./createAction.mdx) may be used directly as the keys here, using
170-
computed property syntax.
171-
172-
```js
173-
const incrementBy = createAction('incrementBy')
174-
175-
createSlice({
176-
name: 'counter',
177-
initialState: 0,
178-
reducers: {},
179-
extraReducers: {
180-
[incrementBy]: (state, action) => {
181-
return state + action.payload
182-
},
183-
'some/other/action': (state, action) => {},
184-
},
185-
})
186-
```
187-
188-
:::tip
189-
190-
We recommend using the `builder callback` API as the default, especially if you are using TypeScript. If you do not use the `builder callback` and are using TypeScript, you will need to use `actionCreator.type` or `actionCreator.toString()` to force the TS compiler to accept the computed property. Please see [Usage With TypeScript](./../usage/usage-with-typescript.md#type-safety-with-extraReducers) for further details.
191-
192-
:::
193-
194154
## Return Value
195155

196156
`createSlice` will return an object that looks like:
@@ -268,14 +228,16 @@ const user = createSlice({
268228
},
269229
},
270230
// "map object API"
271-
extraReducers: {
272-
// @ts-expect-error in TypeScript, this would need to be [counter.actions.increment.type]
273-
[counter.actions.increment]: (
274-
state,
275-
action /* action will be inferred as "any", as the map notation does not contain type information */
276-
) => {
277-
state.age += 1
278-
},
231+
extraReducers: (builder) => {
232+
builder.addCase(
233+
counter.actions.increment,
234+
(
235+
state,
236+
action /* action will be inferred as "any", as the map notation does not contain type information */
237+
) => {
238+
state.age += 1
239+
}
240+
)
279241
},
280242
})
281243

0 commit comments

Comments
 (0)