Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Created June 28, 2016 02:01
Show Gist options
  • Save relaxedtomato/26025044d077701d9ccb4b91343cb0f5 to your computer and use it in GitHub Desktop.
Save relaxedtomato/26025044d077701d9ccb4b91343cb0f5 to your computer and use it in GitHub Desktop.
Combine Reducers
import { combineReducers } from 'redux'

function todos(state = [], action) {
  switch (action.type) {
    case ADD_TODO:
      return [
        ...state,
        {
          text: action.text,
          completed: false
        }
      ]
    case TOGGLE_TODO:
      return state.map((todo, index) => {
        if (index === action.index) {
          return Object.assign({}, todo, {
            completed: !todo.completed
          })
        }
        return todo
      })
    default:
      return state
  }
}

function visibilityFilter(state = SHOW_ALL, action) {
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return action.filter
    default:
      return state
  }
}

const todoApp = combineReducers({
  visibilityFilter,
  todos
})

export default todoApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment