简体   繁体   English

ReactJS - Array中Object键的setState

[英]ReactJS - setState of Object key in Array

So i've been working on this for awhile and felt it would be best to refactor my code so that the state is set up as an array of objects. 所以我一直在研究这个问题,并认为最好重构我的代码,以便将状态设置为一个对象数组。 What i'm trying to do is increment a number on a button click. 我想要做的是按一下按钮增加一个数字。

I have a callback function in a component that triggers a function to update the state...however i'm having difficulty targeting the key value within the object. 我在一个组件中有一个回调函数,它触发一个函数来更新状态......但是我很难在对象中定位键值。

My initial state looks like this: 我的初始状态如下:

getInitialState: function() {
    return {
      items: [
        {
          links: 'zest',
          trackId: 1023,
          songTitle: 'z know the others',
          artist: 'zuvet',
          upVotes: 0
        },
        {
          links: 'alpha',
          trackId: 987,
          songTitle: 'ass',
          artist: 'arme',
          upVotes: 3
        },
      ]
    }

I am trying to target the upVotes key, but can't figure out how. 我试图瞄准upVotes键,但无法弄清楚如何。 My function passes a key so that I can target the index in the array, but when I try to do something like: this.setState({items[key]: {upVotes: this.state.items[key].upVotes + 1}}) it throws an error due to the unexpected [ token. 我的函数传递一个键,以便我可以在数组中定位索引,但是当我尝试执行类似的操作时: this.setState({items[key]: {upVotes: this.state.items[key].upVotes + 1}})由于意外的[令牌this.setState({items[key]: {upVotes: this.state.items[key].upVotes + 1}})它会引发错误。

I have tried something similar to this thread here , but I keep getting errors. 我在这里尝试了类似于这个线程的东西 ,但我一直在收到错误。

What kind of function can I write that will setState of just the key in the object that I want to target? 我可以编写什么样的函数来设置我想要定位的对象中的键的State?

Get current state, modify it and setState() it: 获取当前状态,修改它并setState()它:

var stateCopy = Object.assign({}, this.state);
stateCopy.items[key].upVotes += 1;
this.setState(stateCopy);

Note: This will mutate the state. 注意:这会改变状态。 Here's how to do it without mutation: 以下是没有变异的方法:

var stateCopy = Object.assign({}, this.state);
stateCopy.items = stateCopy.items.slice();
stateCopy.items[key] = Object.assign({}, stateCopy.items[key]);
stateCopy.items[key].upVotes += 1;
this.setState(stateCopy);

It's possible to directly edit the value on your array and set the state to the modified object, considering you're not using immutable.js, that is... 可以直接编辑数组上的值并将状态设置为修改后的对象,考虑到你没有使用immutable.js,那就是......

this.state.array[i].prop = 'newValue';
this.setState({ array: this.state.array });

The problem with direct editing is that React doesn't know the state changed and the update lifecycle doesn't fire. 直接编辑的问题是React不知道状态已更改且更新生命周期未触发。 But setting the state again forces an update. 但是再次设置状态会强制更新。

-- EDIT -- - 编辑 -

If state is Immutable... 如果state是Immutable ......

const array = this.state.array.slice();
array[i].prop = 'newValue';
this.setState({ array });

-- EDIT 2 -- - 编辑2 -

Thanks to the selected answer I realized this would still mutate the element since the array contains only references to the object in question. 由于选择的答案,我意识到这仍然会改变元素,因为数组只包含对相关对象的引用。 Here's a concise ES6-y way to do it. 这是一个简洁的ES6-y方式。

const array = [...this.state.array];
array[i] = { ...array[i], prop: 'New Value' };
this.setState({ array });

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

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