这个redux减速器可以吗 [英] Is this redux reducer OK

查看:73
本文介绍了这个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;
}

如果可以,为什么我们需要所有那些不变的库来使我们的生活复杂化.

and if is OK, why we need all those immutable libraries to complicate our lives.

p.s 只是试图理解Redux和不变性

p.s Just trying to comprehend Redux and immutability

推荐答案

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

I prefer doing this instead. I am not a big fan of switch statements.

这篇关于这个redux减速器可以吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆