javascript - Concat 大数组
问题描述
你好我在react native应用程序上合并大数组时遇到问题,我试图在react native上进行无限滚动,问题是当数组的长度为500+时,每次我添加更多项目我的js线程冻结一秒钟
这是我用来将状态与新项目合并的代码
setData(prev => prev.concat(result));
哪种方法是最好的方法?
编辑:我不是同时渲染 500 个项目,我使用的是一个名为react-native-large-list的库,该库在后台对视图进行所有优化和回收。
解决方案
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)
推荐阅读
- c# - 如何使用 C sharp 在注册表编辑器中设置现有键
- javascript - ASP.NET Core MVC 控制器从 ajax 调用接收输入参数的 null
- javascript - “阅读更多”影响多个盒子 - 如何更改它?(jQuery/JS/CSS)
- r - 在 Fasterize 中选择多个属性?
- azure-active-directory - 如何使用 Microsoft Graph API 根据页码进行分页
- java - 如何扫描 postgreSQL 中的所有数据库?
- navigation - 如何在 Odoo 13 中隐藏“导航菜单”?
- javascript - 在 Material UI 中选择两个项目后自动取消选择“第一个选定项目”
- java - java从其他类获取列表
- javascript - 如何解决boostrap导航栏的问题?