在reducer usint Redux中替换整个状态 [英] Replacing the whole state within a reducer usint Redux

查看:107
本文介绍了在reducer usint Redux中替换整个状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题听起来有线,但如果我从服务器和reducer收集数据会发生什么,我只想返回这些数据并将它们设置为新状态。

the title sounds wired, but what happens if I collect data from a server and in a reducer, I simply want to return these data and set them as the new state.

例如从后端获取数据。如果我想要替换它,我真的需要复制以前的状态:

E.g. fetching data from a backend. Do I really need to make a copy of the previous state if I want to replace it like this:

export function accountData(state = [], action) {
  switch (action.type) {
    case FETCH_FAILURE:
      console.error('SOMETHING BAD JUST HAPPENED');
      break;
    case FETCH_SUCCESS:
        return Object.assign([], state, action.accountData);
    default:
      return state;
  }
}

或者在这种情况下可以做到这个:

or would it be ok in this case to just do this:

export function accountData(state = [], action) {
      switch (action.type) {
        case FETCH_FAILURE:
          console.error('SOMETHING BAD JUST HAPPENED');
          break;
        case FETCH_SUCCESS:
            return action.accountData;
        default:
          return state;
      }
    }


推荐答案

是你可以替换整个州。* Object.assign 只是一种方便的方法来替换部分状态,同时保留其他所有内容,并以不可变的方式这样做。如果你要更换整个州,那么没有必要使用 Object.assign

Yes you can replace the entire state.* Object.assign is simply a convenience method to replace part of the state whilst leaving everything else in tact, and doing so in an immutable fashion. If you're replacing the entire state then it's not necessary to use Object.assign.

(*当我说状态我的意思是减速器负责的状态部分。)

(*When I say state I mean the portion of the state for which the reducer is responsible.)

此外,使用 Object.assign是没有意义的表示一个数组。 Object.assign([],state,action.accountData); 在语义上等同于 Object.assign([],action.accountData ); 也就是说,它会创建一个新的数组副本。但是因为它是一个不是对象的数组,所以写一个更明确和直观的方法是 action.accountData.slice(0) [ ... action.accountData] [更正:实际上, Object.assign 确实是索引-for-index替换数组,这几乎肯定不是你想要的。]

Furthermore it makes no sense to use Object.assign for an array. Object.assign([], state, action.accountData); is semantically equivalent to Object.assign([], action.accountData); That is, it creates a new copy of the array. But since it's an array an not an object, a more explicit and intuitive way to write it would be action.accountData.slice(0) or [...action.accountData]. [CORRECTION: In fact, Object.assign does index-for-index replacement on arrays, which is almost certainly NOT what you want.]

只要你认为数组是只读的,就可以返回数组本身没有复制它。所以你写的最后一种方式 - return action.accountData; - 完全正确。

So long as you treat the array is read-only, you can return the array by itself without copying it. So the final way you've written it - return action.accountData; - is perfectly correct.

最后,我必须解决 FETCH_FAILURE 案例。你没有为此动作返回任何内容。但是你的reducer应该总是返回某些东西。而不是 break; 你应该有返回状态; return []; 具体取决于您想要如何处理失败案例。

Finally, I must take issue with the FETCH_FAILURE case. You're not returning anything for this action. But your reducer should always return something. Instead of break; you should probably have either return state; or return []; depending on exactly how you want to treat the failure case.

这篇关于在reducer usint Redux中替换整个状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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