Skip to content

Commit af460f3

Browse files
authored
Updates documentation for createSlice and createReducer (#460)
1 parent cc8f1ef commit af460f3

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

docs/api/createReducer.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ const counterReducer = createReducer(0, {
3636
```
3737

3838
Action creators that were generated using [`createAction`](./createAction.md) may be used directly as the keys here, using
39-
computed property syntax. (If you are using TypeScript, you may have to use `actionCreator.type` or `actionCreator.toString()`
40-
to force the TS compiler to accept the computed property.)
39+
computed property syntax.
40+
41+
> **Note**: If you are using TypeScript, we recommend using the `builder callback` API that is shown below. If you do not use the `builder callback` and are using TypeScript, you will need to use `actionCreator.type` or `actionCreator.toString()`
42+
> 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.
4143
4244
```js
4345
const increment = createAction('increment')

docs/api/createSlice.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,47 @@ If two fields from `reducers` and `extraReducers` happen to end up with the same
7070
the function from `reducers` will be used to handle that action type.
7171

7272
Action creators that were generated using [`createAction`](./createAction.md) may be used directly as the keys here, using
73-
computed property syntax. (If you are using TypeScript, you may have to use `actionCreator.type` or `actionCreator.toString()`
74-
to force the TS compiler to accept the computed property.)
73+
computed property syntax.
74+
75+
```js
76+
const incrementBy = createAction('incrementBy')
77+
78+
createSlice({
79+
name: 'counter',
80+
initialState: 0,
81+
reducers: {},
82+
extraReducers: {
83+
[incrementBy]: (state, action) => {
84+
return state + action.payload
85+
}
86+
}
87+
})
88+
```
89+
90+
> **Note**: If you are using TypeScript, we recommend using the `builder callback` API that is shown below. If you do not use the `builder callback` and are using TypeScript, you will need to use `actionCreator.type` or `actionCreator.toString()`
91+
> 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.
7592

7693
### The "builder callback" API for `extraReducers`
7794

7895
Instead of using a simple object as `extraReducers`, you can also use a callback that receives a `ActionReducerMapBuilder` instance.
7996

80-
We recommend using this API if stricter type safety is necessary when defining reducer argument objects.
97+
```typescript
98+
const incrementBy = createAction<number>('incrementBy')
99+
100+
createSlice({
101+
name: 'counter',
102+
initialState: 0,
103+
reducers: {},
104+
extraReducers: builder => {
105+
builder.addCase(incrementBy, (state, action) => {
106+
// action is inferred correctly here with `action.payload` as a `number`
107+
return state + action.payload
108+
})
109+
}
110+
})
111+
```
112+
113+
We recommend using this API if stricter type safety is necessary when defining reducer argument objects. It's particularly useful for working with actions produced by `createAction` and `createAsyncThunk`.
81114

82115
## Return Value
83116

@@ -116,6 +149,7 @@ import { createSlice, createAction, PayloadAction } from '@reduxjs/toolkit'
116149
import { createStore, combineReducers } from 'redux'
117150
118151
const incrementBy = createAction<number>('incrementBy')
152+
const decrementBy = createAction<number>('decrementBy')
119153
120154
const counter = createSlice({
121155
name: 'counter',
@@ -128,11 +162,15 @@ const counter = createSlice({
128162
prepare: (value: number) => ({ payload: value || 2 }) // fallback if the payload is a falsy value
129163
}
130164
},
131-
// "builder callback API"
132-
extraReducers: builder =>
165+
// "builder callback API", recommended for TypeScript users
166+
extraReducers: builder => {
133167
builder.addCase(incrementBy, (state, action) => {
134168
return state + action.payload
135169
})
170+
builder.addCase(decrementBy, (state, action) => {
171+
return state - action.payload
172+
})
173+
}
136174
})
137175
138176
const user = createSlice({

0 commit comments

Comments
 (0)