这个redux减速器可以吗

这个减速器可以吗?


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和不变性


HUH函数
浏览 160回答 3
3回答

Smart猫小萌

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语句的忠实拥护者。

慕仙森

我发现了我真正喜欢的东西: 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) => { ... },  });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript