1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
const { createStore } = require("redux");
const types = { ADD_TODO_LIST: "ADD_TODO_LIST", CLEAR_TODO_LIST: "CLEAR_TODO_LIST", INCR_NUM: "INCR_NUM", DECR_NUM: "DECR_NUM", };
function setTodoList(state = [], action) { switch (action.type) { case types.ADD_TODO_LIST: return [...state, action.payload]; case types.CLEAR_TODO_LIST: return []; default: return state; } }
function setNum(state = 0, action) { switch (action.type) { case types.INCR_NUM: return state + 1; case types.DECR_NUM: return state - 1; default: return state; } }
function setAppState(state = {}, action) { return { todoList: setTodoList(state.todoList, action), num: setNum(state.num, action), }; }
const initState = { todoList: [], num: 0, };
const store = createStore(setAppState, initState);
store.subscribe(() => { console.log(">>> state is", store.getState()); });
store.dispatch({ type: types.INCR_NUM }); store.dispatch({ type: types.ADD_TODO_LIST, payload: 1 });
|