繁体   English   中英

反应:更新状态中的对象的属性

[英]React: updating property of object in state

我有一个组件将aDocuments作为状态来跟踪文档。 单击按钮后,将上载文档。 每个文档对象都包含处理上载的功能。 在上载期间,文档中的模式从新更改为完成。 文档对象在上载期间更新模式。 每个文档都是aDocuments数组中处于我状态的对象的引用。

当对象可以自我更新时,如何正确处理该对象的更新属性? 我的setState调用什么都不做,只会导致渲染。

Doc.prototype.upload = function (uploadComplete) {
  if (this.mode === 'new') {
    this.mode = 'uploading';
    // do uploading stuff and call uploadComplete after upload is finished
    $.ajax(..., complete: function() {
      this.mode = 'done';
      uploadComplete(this); 
    });
  }
}

// in my component
clickHandler: function (doc) {

  doc.upload(function(tmpDoc) {
    // this will cause render to show the doc as done
    this.setState('aDocuments', this.state.aDocuments);
  });

  // this will show the document as uploading
  this.setState('aDocuments', this.state.aDocuments);
}

任何帮助将不胜感激。

为了更新文档数组所处的顶级组件的状态,您需要通过prop(修改顶级文档数组)将回调函数传递给各个文档组件。

暂无
暂无

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

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