Skip to content

Instantly share code, notes, and snippets.

@Danielshow
Last active May 31, 2019 23:46
Show Gist options
  • Save Danielshow/902a76cb15ffcdd1b08d10bdbdd0e1ab to your computer and use it in GitHub Desktop.
Save Danielshow/902a76cb15ffcdd1b08d10bdbdd0e1ab to your computer and use it in GitHub Desktop.
import { Types } from './action'
const initialState = {
todos: []
}
const reducer = (state=initialState, action) => {
switch(action.type){
case Types.ADD_TODO: {
const id = state.todos.length + 1
const todo = { id, todo: action.payload }
state.todos.push(todo)
return state
}
case Types.DELETE_TODO: {
const filteredTodos = state.todos.filter((todo) => {
return todo.id !== Number(action.payload)
})
return { todos: filteredTodos };
}
default: return state
}
}
export default reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment