简体   繁体   English

在React中处理对象的状态和数组

[英]Handling state and arrays of objects in React

We all know the React docs say to never mutate this.state directly. 我们都知道React文档说不要直接改变this.state I guess I have a lingering question on state arrays and immutability: 我想我对状态数组和不变性有一个挥之不去的问题:

If I have an array of objects held in state, should I always use the immutability helper as in this question when mutating that array in any way? 如果我在的状态保持对象的数组,应该我总是用不变性助手在为这个问题的变异以任何方式数组时?

Or is it perfectly acceptable to use [].concat() or [].slice() as answered here and here ? 或者在这里这里使用[].concat()[].slice()完全可以接受?

I ask this question again because [].concat() and [].slice() do return new arrays, but only shallow copy arrays. 我再次问这个问题,因为[].concat()[].slice()确实返回新的数组,但只返回浅拷贝数组。 If I change an element of the array, it will change the array in state as well, violating the first rule of Reactjs State (I've been watching too much FMA:Brotherhood): 如果我改变数组的一个元素,它也会改变状态中的数组,违反了Reactjs State的第一条规则(我一直在看太多的FMA:兄弟会):

var arr1 = [{ name : "bill" }, { name : "chet" }];
var arr2 = arr1.slice();

// this changes both arrays
arr2[0].name = "kevin";

// check
(arr1[0].name === arr2[0].name) // => true; both are "kevin"
(arr1[0] === arr2[0])           // => true; only shallow copy

// this changes arr2 only
arr2.push({ name : "alex" });

// check again
(arr1.length === arr2.length)   // => false;
(arr1[0].name === arr2[0].name) // => still true;
(arr1[0] === arr2[0])           // => still true;

I understand that the addon update is most commonly used when overriding shouldComponentUpdate , but for what I'm doing I don't need to override that function; 我知道在覆盖shouldComponentUpdate时最常使用插件update ,但是对于我正在做的事情,我不需要覆盖该函数; I just need to mutate objects in the array held in state by either adding new elements to the array (solved using concat or slice), or by changing properties of existing elements (solved by using React.addons.update). 我只需要通过向数组添加新元素(使用concat或slice解决)或通过更改现有元素的属性(使用React.addons.update解决)来改变状态中保存的数组中的对象。

TL;DR TL; DR

If not overriding shouldComponentUpdate , when should I use React.addons.update over [].slice() or [].concat() to mutate an array of objects stored in state? 如果不重写shouldComponentUpdate ,何时应该在[].slice()[].concat()上使用React.addons.update来改变存储在状态中的对象数组?

You primarily need to consider boundaries. 您主要需要考虑边界。 If you only use this array in the component that owns it, and optionally pass it down as props: it doesn't matter. 如果您只在拥有它的组件中使用此数组,并可选择将其作为props传递:它无关紧要。 You can modify it in any way, and unless the other components have some serious problems, they'll never know the difference because they get the new props when your component updates its state. 您可以以任何方式对其进行修改,除非其他组件存在严重问题,否则他们永远不会知道差异,因为当组件更新其状态时,它们会获得新的道具。

If you pass this array to some kind of api, such as a flux action creator or a function passed to you as a prop, then you might run into some very serious and hard to track down bugs. 如果你将这个数组传递给某种api,例如一个助手动作创建者或作为道具传递给你的函数,那么你可能会遇到一些非常严重且难以追踪的错误。 This is where immutability is important. 这是不变性很重要的地方。

For example, in this case you might want to check some property of a person in the callback, however if it's possible for people[0].name to have changed, you have a race condition. 例如,在这种情况下,您可能想要检查回调中某人的某些属性,但是如果people[0].name可能已更改,则您有竞争条件。

function savePeople(people){
  $.post('/people/update', people, function(resp){
    if (people[0].name === ...) {
      // ...
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM