Skip to content

Commit c5dabec

Browse files
committed
make the slice the reducer function
1 parent c58aa2c commit c5dabec

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

docs/api/createSlice.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,19 @@ to force the TS compiler to accept the computed property.)
6969

7070
## Return Value
7171

72-
`createSlice` will return an object that looks like:
72+
`createSlice` will return a reducer function with these additional properties:
7373

7474
```ts
7575
{
7676
name : string,
77-
reducer : ReducerFunction,
7877
actions : Object<string, ActionCreator},
7978
}
8079
```
8180

8281
Each function defined in the `reducers` argument will have a corresponding action creator generated using [`createAction`](./createAction.md)
8382
and included in the result's `actions` field using the same function name.
8483

85-
The generated `reducer` function is suitable for passing to the Redux `combineReducers` function as a "slice reducer".
84+
The generated function is suitable for passing to the Redux `combineReducers` function as a "slice reducer".
8685

8786
You may want to consider destructuring the action creators and exporting them individually, for ease of searching
8887
for references in a larger codebase.
@@ -129,8 +128,8 @@ const user = createSlice({
129128
})
130129

131130
const reducer = combineReducers({
132-
counter: counter.reducer,
133-
user: user.reducer
131+
counter,
132+
user
134133
})
135134

136135
const store = createStore(reducer)

src/createSlice.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ describe('createSlice', () => {
3535
})
3636

3737
describe('when passing slice', () => {
38-
const { actions, reducer } = createSlice({
38+
const reducer = createSlice({
3939
reducers: {
4040
increment: state => state + 1
4141
},
4242
initialState: 0,
4343
name: 'cool'
4444
})
45+
const { actions } = reducer
46+
47+
it('should have the correct name', () => {
48+
expect(reducer.name).toBe('cool')
49+
})
4550

4651
it('should create increment action', () => {
4752
expect(actions.hasOwnProperty('increment')).toBe(true)
@@ -62,7 +67,7 @@ describe('createSlice', () => {
6267
describe('when mutating state object', () => {
6368
const initialState = { user: '' }
6469

65-
const { actions, reducer } = createSlice({
70+
const reducer = createSlice({
6671
reducers: {
6772
setUserName: (state, action) => {
6873
state.user = action.payload
@@ -71,6 +76,7 @@ describe('createSlice', () => {
7176
initialState,
7277
name: 'user'
7378
})
79+
const { actions } = reducer
7480

7581
it('should set the username', () => {
7682
expect(reducer(initialState, actions.setUserName('eric'))).toEqual({
@@ -82,7 +88,7 @@ describe('createSlice', () => {
8288
describe('when passing extra reducers', () => {
8389
const addMore = createAction('ADD_MORE')
8490

85-
const { reducer } = createSlice({
91+
const reducer = createSlice({
8692
name: 'test',
8793
reducers: {
8894
increment: state => state + 1,
@@ -137,7 +143,7 @@ describe('createSlice', () => {
137143
}
138144
})
139145

140-
testSlice.reducer(0, testSlice.actions.testReducer('testPayload'))
146+
testSlice(0, testSlice.actions.testReducer('testPayload'))
141147
expect(reducer).toHaveBeenCalledWith(
142148
0,
143149
expect.objectContaining({ payload: 'testPayload' })

src/createSlice.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ export type SliceActionCreator<P> = PayloadActionCreator<P>
1919
export interface Slice<
2020
State = any,
2121
ActionCreators extends { [key: string]: any } = { [key: string]: any }
22-
> {
22+
> extends Reducer<State> {
2323
/**
2424
* The slice name.
2525
*/
2626
name: string
2727

28-
/**
29-
* The slice's reducer.
30-
*/
31-
reducer: Reducer<State>
32-
3328
/**
3429
* Action creators for the types of actions that are handled by the slice
3530
* reducer.
@@ -198,9 +193,9 @@ export function createSlice<
198193
{} as any
199194
)
200195

201-
return {
202-
name,
203-
reducer,
196+
Object.defineProperty(reducer, 'name', { value: name, writable: false })
197+
198+
return Object.assign(reducer, {
204199
actions: actionMap
205-
}
200+
})
206201
}

type-tests/files/createSlice.typetest.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ function expectType<T>(t: T) {
2626

2727
/* Reducer */
2828

29-
const reducer: Reducer<number, PayloadAction> = slice.reducer
29+
const reducer: Reducer<number, PayloadAction> = slice
3030

3131
// typings:expect-error
32-
const stringReducer: Reducer<string, PayloadAction> = slice.reducer
32+
const stringReducer: Reducer<string, PayloadAction> = slice
3333
// typings:expect-error
34-
const anyActionReducer: Reducer<string, AnyAction> = slice.reducer
34+
const anyActionReducer: Reducer<string, AnyAction> = slice
3535

3636
/* Actions */
3737

0 commit comments

Comments
 (0)