简体   繁体   English

如何正确修改输入值并同时修改数组值?

[英]How to modify input value correctly and modify array value at the same time?

I render the values that I have inside an object and an input next to each of them with that object index item as value.我使用对象索引项作为值呈现对象内部的值和每个对象旁边的输入。 When I try to use the onChange prop, I cannot modify neither the rendered value nor the input value.当我尝试使用 onChange 道具时,我无法修改渲染值和输入值。 How can I do this changing the state of the element in the array?我怎样才能改变数组中元素的状态?

I want the Toto value to be modified as I change the input value我希望在更改输入值时修改 Toto 值

我希望在更改输入值时修改 Toto 值

My object players looks like this:我的对象播放器如下所示:

[
  {
  "name": "Toto";
  },
  {
   "name": "Lolz",
  }
]

Here, I try to render my table:在这里,我尝试渲染我的表格:

modifyItem = (event, index) => {
   this.state.players[index].name = event.target.value
   //my problem is clearly here
}

render() {
    const playersList = [...new Array(this.state.players.length)].map((it, index) => {
    return (
    <tr key={index}>
      <td>{this.state.players[index].name}</td>
        <input type="text" value={this.state.players[index].name} onChange={this.modifyItem}/>
      </td>
    </tr>
    )
    })

    return () {
      <div>
         {playersList}
      </div>
    }

What I want to do is:我想做的是:

In each element of the table AND input ( for example the first one Toto), I want to modify this value in the input and consecutively in the table.在表AND输入的每个元素(例如第一个Toto)中,我想在输入中并在表中连续修改此值。 I can't seem to find the correct answer.我似乎无法找到正确答案。

I've created a codesandbox example for you here:我在这里为您创建了一个代码沙盒示例:

https://codesandbox.io/s/vnkoxop6z3 https://codesandbox.io/s/vnkoxop6z3

You need to store your values in state, and when you modify each item you need to know what the new value is via the onChange callback, and the index of that current value.您需要将值存储在状态中,当您修改每个项目时,您需要通过 onChange 回调知道新值是什么,以及该当前值的索引。

From here you can duplicate the array by spreading it into a new one, access that particular index item on the new array, and update the name with the new value.从这里您可以通过将数组扩展为一个新数组来复制数组,访问新数组上的特定索引项,并使用新值更新名称。

Once you have done this.一旦你这样做了。 setState with the new array.使用新数组设置状态。

class Component extends React.Component {
  constructor() {
    super();
    this.state = {
      values: [
        {
          name: "Toto"
        },
        {
          name: "Lolz"
        }
      ]
    };
  }

  modifyItem(e, index) {
    const newValue = e.target.value;
    const values = [...this.state.values];
    values[index].name = newValue;

    this.setState({
      values
    });
  }

  render() {
    return this.state.values.map((value, index) => {
      return (
        <Fragment>
          <label>{value.name}</label>
          <input
            type="text"
            value={value.name}
            onChange={e => this.modifyItem(e, index)}
          />
        </Fragment>
      );
    });
  }
}

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

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