简体   繁体   English

Mobx可观察阵列未更新

[英]Mobx Observable Array not updated

I am declaring a observable array in the following way in reactjs using mobx 我使用mobx在reactjs中以以下方式声明一个可观察的数组

@observable cacheditems

constructor() {
    this.cacheditems  =   []

Now I am retrieving the data from pouch-db when offline as follows: 现在,我离线时从pouch-db检索数据,如下所示:

var items = []
db.allDocs({include_docs: true}, function(err, docs) {
                docs.rows.map((obj, id) => {
                    items.push(obj.doc)
                })
            })


 this.cacheditems = items

But the data is not set. 但是没有设置数据。 When I try to get the data for rendering its a empty array. 当我尝试获取数据以呈现其空数组时。

When you do this.cacheditems = items you are overwriting the reference to the observable array. 当您执行this.cacheditems = items您将覆盖对可观察数组的引用。 You can use replace instead: 您可以使用replace代替:

class Store {
  @observable cacheditems = []

  constructor() {
    db.allDocs({include_docs: true}, (err, docs) => {
      var items = []
      docs.rows.map((obj, id) => {
        items.push(obj.doc)
      })
      this.cacheditems.replace(items)
    })
  }
}

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

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