首页 > 解决方案 > Concat 大数组

问题描述

你好我在react native应用程序上合并大数组时遇到问题,我试图在react native上进行无限滚动,问题是当数组的长度为500+时,每次我添加更多项目我的js线程冻结一秒钟

这是我用来将状态与新项目合并的代码

setData(prev => prev.concat(result));

哪种方法是最好的方法?

编辑:我不是同时渲染 500 个项目,我使用的是一个名为react-native-large-list的库,该库在后台对视图进行所有优化和回收。

标签: javascriptarraysreactjsreact-nativeoptimization

解决方案


Array.concat creates a new array, copying all values and object references into the new array.. That means, if you have an array with 500 items and you want to add 500 more items, you have to allocate 1000 more items into memory.

You would be better off just adding your new items to the existing array:

prev.push(...result)


推荐阅读