簡體   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