Javascript ReactJS:仅更新嵌套状态对象中的特定字段

Javascript ReactJS:仅更新嵌套状态对象中的特定字段,javascript,reactjs,Javascript,Reactjs,我需要更新特定对象字段的状态。我的状态正在使用动态键值(索引) 首先,我在做: this.setState({ [index]: { uploading: uploadInstance, progress: 0 } }) 现在我只需要更新进度字段。尝试上载时,字段丢失: this.setState({ [index]: { progress: progress } }) 在this.state[index]处复制对象,并用新属性替换进度属性 co

我需要更新特定对象字段的状态。我的状态正在使用动态键值(
索引

首先,我在做:

this.setState({
  [index]: {
    uploading: uploadInstance,
    progress: 0
  }
})
现在我只需要更新进度字段。尝试上载时,
字段丢失:

this.setState({ 
  [index]: { 
    progress: progress 
  }
})

this.state[index]
处复制对象,并用新属性替换进度属性

const updatedOne = { ...this.state[index], progress: someNewProgress };
this.setState({ [index]: updatedOne });
这样,将保留对象中以前的属性,并用新属性替换进度

如果您不支持扩展运算符,则可以使用
Object.assign

const updatedOne = Object.assign({}, this.state[index], { progress: someNewProgress });

this.state[index]
处复制对象,并用新属性替换进度属性

const updatedOne = { ...this.state[index], progress: someNewProgress };
this.setState({ [index]: updatedOne });
这样,将保留对象中以前的属性,并用新属性替换进度

如果您不支持扩展运算符,则可以使用
Object.assign

const updatedOne = Object.assign({}, this.state[index], { progress: someNewProgress });

我认为React不是设计用来处理动态场的。为什么必须使用动态字段?您想尝试其他方法吗?我认为React不是为处理动态字段而设计的。为什么必须使用动态字段?你想尝试其他选择吗?