Skip to content

Commit

Permalink
Merge pull request reduxjs#985 from sirbrillig/patch-1
Browse files Browse the repository at this point in the history
Add missing reducer composition example to docs
  • Loading branch information
omnidan committed Oct 30, 2015
2 parents de2d11d + 90ef233 commit bd7b6c9
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions docs/basics/Reducers.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,32 +199,41 @@ function todoApp(state = initialState, action) {
Is there a way to make it easier to comprehend? It seems like `todos` and `visibilityFilter` are updated completely independently. Sometimes state fields depend on one another and more consideration is required, but in our case we can easily split updating `todos` into a separate function:

```js
function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
...state,
{
text: action.text,
completed: false
}
]
case COMPLETE_TODO:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
]
default:
return state
}
}

function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
case ADD_TODO:
return Object.assign({}, state, {
todos: [
...state.todos,
{
text: action.text,
completed: false
}
]
})
case COMPLETE_TODO:
return Object.assign({}, state, {
todos: [
...state.todos.slice(0, action.index),
Object.assign({}, state.todos[action.index], {
completed: true
}),
...state.todos.slice(action.index + 1)
]
})
return {
...state,
todos: todos(state.todos, action)
};
default:
return state
}
Expand Down

0 comments on commit bd7b6c9

Please sign in to comment.