简体   繁体   中英

Mobx Observable Array not updated

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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