为了账号安全,请及时绑定邮箱和手机立即绑定

这个redux减速器可以吗

这个redux减速器可以吗

HUH函数 2021-04-08 13:15:17
这个减速器可以吗?function someReducer(state = initialState, action) {   if (action.type === SOME_ACTION) {      const newState = Object.assign( {}, state );      // ...      // doing whatever I want with newState       // ...      return newState;   }      return state;}如果可以,为什么我们需要所有这些不变的库来使我们的生活复杂化。ps只是试图理解Redux和不变性
查看完整描述

3 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

export default function (state = initialState, action) {


  const actions = {

    SOME_ACTION: () => {

      return {

        ...state

      }

    },

    ANOTHER_ACTION: () => {

      return {

        ...state

        error: action.error

      }

    },

    DEFAULT: () => state;

  }

  

  return actions[action.type] ? actions[action.type]() : actions.DEFAULT(); 

}

我更喜欢这样做。我不是switch语句的忠实拥护者。


查看完整回答
反对 回复 2021-04-22
?
慕仙森

TA贡献1827条经验 获得超8个赞

我发现了我真正喜欢的东西:


 import createReducer from 'redux-starter-kit';

 const someReducer = createReducer( initialState, {

    SOME_ACTION: (state) => { /* doing whatever I want with this local State */ },

    SOME_ANOTHER_ACTION: (state) => { /* doing whatever I want with local State */ },

    THIRD_ACTION: (state, action) => { ... }, 

 });


查看完整回答
反对 回复 2021-04-22
  • 3 回答
  • 0 关注
  • 159 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信