Mobx Observable Array not updated

限于喜欢 提交于 2019-12-11 02:29:45

问题


I am declaring a observable array in the following way in reactjs using mobx

@observable cacheditems

constructor() {
    this.cacheditems  =   []

Now I am retrieving the data from pouch-db when offline as follows:

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.


回答1:


When you do this.cacheditems = items you are overwriting the reference to the observable array. You can use replace instead:

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)
    })
  }
}


来源:https://stackoverflow.com/questions/44986339/mobx-observable-array-not-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!