store.js 835 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { combineReducers, createStore } from "redux";
  2. const addTodo = (todo) => ({
  3. type: "ADD_TODO",
  4. todo,
  5. });
  6. const removeTodo = (id) => ({
  7. type: "REMOVE_TODO",
  8. id,
  9. });
  10. const initialState = {
  11. todos: [],
  12. };
  13. const handleNewTodo = (state, action) => ({
  14. todos: [...state.todos, action.todo],
  15. });
  16. const handleRemoveTodo = (state, action) => ({
  17. todos: [
  18. ...state.todos.slice(0, action.id),
  19. ...state.todos.slice(action.id + 1),
  20. ],
  21. });
  22. const currentList = (state = initialState, action) => {
  23. const handlers = {
  24. ADD_TODO: handleNewTodo,
  25. REMOVE_TODO: handleRemoveTodo,
  26. };
  27. return handlers[action.type] ? handlers[action.type](state, action) : state;
  28. };
  29. const rootReducer = combineReducers({
  30. currentList,
  31. });
  32. const store = createStore(rootReducer);
  33. export {
  34. addTodo,
  35. removeTodo,
  36. store,
  37. };