reactjs - 反应原生。VirtualizedList:PureComponent、shouldComponentUpdate等
问题描述
我从 API 得到一个大数组,我想显示这个数组中的图像。我做到了,但它不能正常工作。
React native 仅显示 8 张图像并向我发送下一个信息(VirtualizedList:您有一个更新缓慢的大型列表 - 确保您的 renderItem 函数呈现遵循 React 性能最佳实践的组件,如 PureComponent、shouldComponentUpdate 等)
除了 React Native 的推荐,我如何显示所有图像?
任何人都可以帮助我吗?提前致谢!
我的代码:
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, View, Image, SafeAreaView, StyleSheet } from 'react-native';
export default SignInScreen = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/photos')
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
});
return (
<SafeAreaView>
{isLoading ? <ActivityIndicator/> : (
<View style={styles.container}>
<FlatList
data={data}
keyExtractor={({ id }, index) => id.toString()}
renderItem={({ item }) => <Image style={styles.imageView} source={{ uri: item.thumbnailUrl }} />}
/>
</View>
)}
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
paddingVertical: 60,
paddingHorizontal: 20,
},
imageView: {
width: 150,
height: 150 ,
margin: 7,
borderRadius : 7
}
})
解决方案
使用 PureComponent 在 FlatList 中创建一个组件,这样当图像数组没有变化时,FlatList 就不会重新渲染。
推荐阅读
- c++ - 如何将拆分源添加到 CMake?
- javascript - 如何在jsPDF中添加动态内容?
- flutter - 在与 bloc 一起颤动中按子字符串搜索
- java - 如何使用 throw 在没有错误的地方创建错误?
- python-3.x - 有没有办法在 Pillow 中显示小于实际屏幕尺寸的图像?
- java - 我正在尝试在停机时间做一个数组,但我需要尝试将 2 个列表链接在一起以将它们打印出来
- java - Apache Poi - createCell() 方法不接受通过 for 循环的索引作为参数
- python - 如何在 Python Importing Stocks 数据中解决这个问题?
- phaser-framework - 如何将移相器游戏部署到网络或如何将其导出
- amazon-web-services - cloudFormation 模板验证错误:如何拆分 serverless.yml 文件