Last active
May 31, 2019 23:46
-
-
Save Danielshow/902a76cb15ffcdd1b08d10bdbdd0e1ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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