Skip to content

Commit a148ad1

Browse files
authored
Merge pull request #3702 from reduxjs/create-reducer-payload
Switch advised syntax for create.reducer
2 parents 898ed5a + 04085b3 commit a148ad1

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

docs/api/createSlice.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ The main benefit of this is that you can create [async thunks](./createAsyncThun
142142

143143
```ts title="Creator callback for reducers"
144144
import { createSlice, nanoid } from '@reduxjs/toolkit'
145-
import type { PayloadAction } from '@reduxjs/toolkit'
146145
147146
interface Item {
148147
id: string
@@ -161,7 +160,7 @@ const todosSlice = createSlice({
161160
todos: [],
162161
} as TodoState,
163162
reducers: (create) => ({
164-
deleteTodo: create.reducer((state, action: PayloadAction<number>) => {
163+
deleteTodo: create.reducer<number>((state, action) => {
165164
state.todos.splice(action.payload, 1)
166165
}),
167166
addTodo: create.preparedReducer(
@@ -209,7 +208,7 @@ A standard slice case reducer.
209208
- **reducer** The slice case reducer to use.
210209

211210
```ts no-transpile
212-
create.reducer((state, action: PayloadAction<Todo>) => {
211+
create.reducer<Todo>((state, action) => {
213212
state.todos.push(action.payload)
214213
})
215214
```

packages/toolkit/src/tests/createSlice.typetest.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -599,18 +599,14 @@ const value = actionCreators.anyKey
599599
}>()
600600

601601
return {
602-
normalReducer: create.reducer(
603-
(state, action: PayloadAction<string>) => {
604-
expectType<TestState>(state)
605-
expectType<string>(action.payload)
606-
}
607-
),
608-
optionalReducer: create.reducer(
609-
(state, action: PayloadAction<string | undefined>) => {
610-
expectType<TestState>(state)
611-
expectType<string | undefined>(action.payload)
612-
}
613-
),
602+
normalReducer: create.reducer<string>((state, action) => {
603+
expectType<TestState>(state)
604+
expectType<string>(action.payload)
605+
}),
606+
optionalReducer: create.reducer<string | undefined>((state, action) => {
607+
expectType<TestState>(state)
608+
expectType<string | undefined>(action.payload)
609+
}),
614610
noActionReducer: create.reducer((state) => {
615611
expectType<TestState>(state)
616612
}),
@@ -788,7 +784,7 @@ const value = actionCreators.anyKey
788784
start: create.reducer((state) => {
789785
state.status = 'loading'
790786
}),
791-
success: create.reducer((state, action: PayloadAction<T>) => {
787+
success: create.reducer<T>((state, action) => {
792788
state.data = castDraft(action.payload)
793789
state.status = 'finished'
794790
}),

0 commit comments

Comments
 (0)