需求:小程序触底刷新加载更多,列表很长时如何局部刷新列表?(只刷新新增部分)
let oldList
= [1, 2, 3]
let addList
= [4, 5]
let newList
= [1, 2, 3, 4, 5]
问题具象化: 在 setData() 时,只刷新 addList 而非刷新整个的 newList
旧的解决方式:
newList
= oldList
.concat(addList
)
this.setData({
oldList
: newList
})
新的解决方法:
let index
= this.data
.oldList
.length
;
addList
.forEach(ele
=> {
this.setData({
[`oldList[${index++}]`]: ele
})
});
说明: 以上代码完成了新增列表addList的局部刷新