javascript - React Native FlatList - 多次重新渲染/安装子项目
问题描述
https://snack.expo.io/SykFiqQ8E
我一直在编写一个使用 FlatList ( https://facebook.github.io/react-native/docs/flatlist ) 的 react-native 应用程序。我注意到列表中的子项存在一些性能问题,如果我向数据集添加更多数据,FlatList 组件将多次重新渲染/安装子项。该事实对用户不可见,但在堆栈跟踪中,我能够证明它多次渲染该子项,只需在每次渲染时执行警报即可。
查看问题的现场演示,然后单击添加项目。它会在每个项目中多次提醒子项目。我不知道我的实现是否错误,或者我是否需要在 react native 的 github 上创建问题。
import * as React from 'react';
import { Text, View, StyleSheet, FlatList, Alert, Button } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
state = {
data: [{key: 'i love rn but why do my items render/mount more than once'}]
}
addItem() {
const data = this.state.data.concat({key: "item#" + this.state.data.length.toString()})
this.setState({ data })
}
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.data}
renderItem={({item}) => <Text>{item.key} {Alert.alert(item.key)}</Text>}
/>
<Button onPress={this.addItem.bind(this)} title="Add Item" />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
}
});
我不确定他们的反应速度有多快,所以希望在这里获得一些反馈。
解决方案
推荐阅读
- sql - 如何根据最近的日期属性获取两个字段?
- cakephp - 根据控制器禁用 debug_kit
- git - 如何根据最新的 git describe 命令自动设置我的 Inno Setup 安装程序的版本?
- mysql - 在一个大的 mySQL 表上选择会引发所有连接切换到等待处理程序提交的状态
- c - 为什么 ARM CMSIS I2C 读取功能不起作用?
- node.js - npm WARN read-shrinkwrap 这个版本的 npm 兼容 lockfileVersion@1,但是 package-lock.json 是为 lockfileVersion@0 生成的
- c# - UWP 重新启动应用程序列表
- python - 为什么我的画布图像没有使用 Tkinter 和 Pillow 显示?
- swift - 无法迁移具有属性类型更改的核心数据模型
- python - 将csv文件转换为json文件