需求:小程序触底刷新加载更多,列表很长时如何局部刷新列表?(只刷新新增部分)
let oldList = [1, 2, 3]
let addList = [4, 5]
let newList = [1, 2, 3, 4, 5]
// oldList + addList = newList
问题具象化: 在 setData() 时,只刷新 addList 而非刷新整个的 newList
旧的解决方式:
// 代码块1
newList = oldList.concat(addList)
this.setData({
oldList: newList
})
// 刷新了整个列表,资源浪费
新的解决方法:
// 代码块2
let index = this.data.oldList.length;
addList.forEach(ele => {
this.setData({
[`oldList[${index++}]`]: ele
})
});
// 部分刷新列表,高效简洁
说明: 以上代码完成了新增列表addList的局部刷新
版权声明:本文为weixin_43543882原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。