reducer 里为什么要用switch 来判断action?
来源:5-5 Action 和 Reducer 的编写
 
			hood
2018-09-30
用switch 有什么好处吗? 为什么好多教程都是用switch,想下面这种用对象属性判断感觉更方便啊。
const defaultAction = {
  change_input_value(state, action) {
      state.inputValue = action.value
  },
  add_todo_item(state) {
    state.list.push(state.inputValue);
    state.inputValue = '';
  }
}
export default (state = defaultState, action) => {
  if (defaultAction[action.type]) {
      const newState = JSON.parse(JSON.stringify(state));
      defaultAction[action.type](newState, action);
      return newState;
  }
  return state;
}
写回答
	1回答
- 
				  Dell 2018-10-01 你也可以用if,无所谓哇,switch可以让多个判断语法看起来更简单 00
相似问题
