import { combineReducers, createStore } from "redux"; const addTodo = (todo) => ({ type: "ADD_TODO", todo, }); const removeTodo = (id) => ({ type: "REMOVE_TODO", id, }); const initialState = { todos: [], }; const handleNewTodo = (state, action) => ({ todos: [...state.todos, action.todo], }); const handleRemoveTodo = (state, action) => ({ todos: [ ...state.todos.slice(0, action.id), ...state.todos.slice(action.id + 1), ], }); const currentList = (state = initialState, action) => { const handlers = { ADD_TODO: handleNewTodo, REMOVE_TODO: handleRemoveTodo, }; return handlers[action.type] ? handlers[action.type](state, action) : state; }; const rootReducer = combineReducers({ currentList, }); const store = createStore(rootReducer); export { addTodo, removeTodo, store, };