小程序触底刷新加载更多,如何局部刷新列表?

    技术2025-02-13  12

    需求:小程序触底刷新加载更多,列表很长时如何局部刷新列表?(只刷新新增部分)

    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的局部刷新

    Processed: 0.012, SQL: 9