Redux action的使用

84 阅读1分钟

#Action

1.action是一个plain-object(平面对象) 它的_proto_指向Object.prototype 2.action中必须有type属性,该属性用于描述操作的类型(对type类型无强制性要求) 3.为了方便传递action,使用createAction来创建action action创建函数为纯函数:不能以任何形式改动参数,不可以异步操作,不可以对外部环境中的数据造成影响 4.bindActionCreators:用于增强action创建函数的功能,不仅可以创建action,且创建后自动分发

const INCREASE = Symbol("increase");
const DECREASE = Symbol("decrease");
const SET = Symbol("set");
 //得到一个用于增加数字操作的action
 function getIncreace() {
    return {
      type:actionTypes.INCREASE
    };
 }
 function getDecrease() {
    return {
      type:actionTypes.DECREASE
    };
 }
 function getSet() {
    return {
      type:actionTypes.SET,
      payload:nueNumber
    };
 }
  
  //state:之前仓库中的状态
  //action:要操作的对象
 function reducer(state,action){
    if(action.type === actionTypes.INCREASE){
      return state + 1;
    }
    else if (action.type === actionTypes.DECREASE) {
        return state - 1;
    }
     else if (action.type === actionTypes.SET) {
        return action.payload;
    }
    retuen state;
  }
  //创建store函数
  const store = createStore(reducer,10);
  //用于增强action创建函数的功能,不仅可以创建action,且创建后自动分发
  const boundActions = bindActionCreators(numberActions,store.dispatch);
  //得到一个increacr action,直接分发
  boundActions.getIncreace();