reducer.js 1.02 KB
Newer Older
mayi's avatar
mayi committed
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
import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM } from "./store/actionsTypes";
const defaultState = {
  inputValue: "Write Something修改版",
  list: ["数组第一条", "数组第二条", "数组第三条", "数组第四条"],
};
export const reducer = (state = defaultState, action) => {
  //   console.log(state, action);

  //reducer 里 只能接收state,不能改变state
  if (action.type === CHANGE_INPUT) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }
  if (action.type === ADD_ITEM) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.list.push(newState.inputValue);
    newState.inputValue = "";
    // console.log("newState.inputValue", "2" + newState.inputValue + "2");
    // console.log("newState.list", newState.list);
    return newState;
  }
  if (action.type === DELETE_ITEM) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.list.splice(action.index, 1);
    return newState;
  }
  return state;
};

//仓库