score:2

Accepted answer
const initialState = [
  { text: 'Audi', id: 1, completed: false },
  { text: 'Nissan', id: 2, completed: false },
  { text: 'Mazda', id: 3, completed: false }
]

const todos = (state = initialState, action) => {
  switch (action.type) {
  case 'ADD_TODO':
    return [
      ...state,
      {
        id: action.id,
        text: action.text,
        completed: false
      }
    ]
  case 'TOGGLE_TODO':
    return state.map(todo =>
      (todo.id === action.id) 
        ? {...todo, completed: !todo.completed}
        : todo
    )
  default:
    return state
  }
}

export default todos

Related Query

More Query from same tag