Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不使用硬编码的情况下将数组的索引传递给';react插件更新';?_Javascript_Reactjs_Immutability - Fatal编程技术网

Javascript 如何在不使用硬编码的情况下将数组的索引传递给';react插件更新';?

Javascript 如何在不使用硬编码的情况下将数组的索引传递给';react插件更新';?,javascript,reactjs,immutability,Javascript,Reactjs,Immutability,如果我将索引硬编码到任何数字,则上述代码正在工作,即(update(this.state.persons,{0:{name:{$set:updatedName}}}})) 请建议解决方案。将{index:…}替换为{[index]:…}您可以使用a来使用索引变量的值: nameChangedHandler = (id, event) => { let index = id; const updatedName = event.target.value; this.s

如果我将索引硬编码到任何数字,则上述代码正在工作,即(
update(this.state.persons,{0:{name:{$set:updatedName}}}})


请建议解决方案。

{index:…}
替换为
{[index]:…}

您可以使用a来使用
索引
变量的值:

nameChangedHandler = (id, event) => {
    let index = id;
    const updatedName = event.target.value;
    this.setState({
      persons: update(this.state.persons, { index: { name: { $set: updatedName } } }),
    });
  };

为什么只有索引必须作为计算属性传递?为什么不更新名称?@AravindVijayan
{key:value}
的右侧始终是该变量的值。
this.setState({
  persons: update(this.state.persons, { [index]: { name: { $set: updatedName } } }),
});