Mobx可观察阵列未更新 [英] Mobx Observable Array not updated
本文介绍了Mobx可观察阵列未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用mobx在reactjs中以以下方式声明一个可观察的数组
I am declaring a observable array in the following way in reactjs using mobx
@observable cacheditems
constructor() {
this.cacheditems = []
现在,我正在离线时从pouch-db检索数据,如下所示:
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.
推荐答案
执行this.cacheditems = items
时,您将覆盖对可观察数组的引用.您可以改用 替换 :
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)
})
}
}
这篇关于Mobx可观察阵列未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文