【发布时间】:2018-10-11 09:49:08
【问题描述】:
请帮助我。当列表很大时,平面列表会滞后,有时会在 react native 中崩溃。
我尝试过优化列表:它不显示图像,我尝试过 react-native 大列表。没有任何效果。
这是代码:
{loading == false && (
<FlatList
data={this.state.data}
numColumns="2"
renderItem={({ item }) => <ExampleComponent url=
{item.media} />}
keyExtractor={(item, index) => item.itemurl}
windowSize={15}
onEndReachedThreshold={1}
onEndReached={this.handleLoadMore}
removeClippedSubviews={true}
maxToRenderPerBatch={10}
/>
)}
这是组件类
import React, { PureComponent } from "react";
import { View, Text, StyleSheet, Image ,TouchableOpacity } from "react-native";
import FastImage from "react-native-fast-image";
export default class ExampleComponent extends PureComponent {
constructor(props) {
super(props);
//console.log(this.props);
}
render() {
return (
<TouchableOpacity style={styles.imageContainer}>
<View >
<FastImage
source={{
uri:this.props.url
}}
style={styles.imageStyle}
/>
</View>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
imageContainer: {
flex: 1,
borderWidth: 1,
borderColor: "#dedede",
borderRadius: 5,
margin: 5
},
imageStyle: {
flex: 1,
width: null,
height: 100,
borderTopLeftRadius:5,
borderTopRightRadius:5
},
});
And im getting this message in console when rows are more
这是处理加载更多功能
handleLoadMore = () => {
console.log("Called");
if (this.state.next != 0) {
const u =
"https://url&q=" +
this.props.navigation.state.params.data +
"&pos=" +
this.state.next;
fetch(u, {
method: "GET"
})
.then(res => res.json())
.then(response => {
this.setState({
data: [...this.state.data, ...response.results],
next: response.next
});
});
}
}
VirtualizedList:您有一个更新缓慢的大型列表 - 确保您的 renderItem 函数呈现遵循 React 性能最佳实践的组件,例如 PureComponent、shouldComponentUpdate 等。 {"dt":1098,"prevDt":684,"内容长度":6832}
【问题讨论】:
-
尝试删除 initialNumToRender 并为您的用例调整 windowSize。此外,您可以尝试 legacyImplementation={true} 并玩弄
-
感谢您的回复,我现在自己试试
-
多大是多大?您要渲染多少个项目? Flatlist 是已经为 React Native 优化的列表。请同时添加 ExampleComponent 的代码。
-
100 多行后的项目数量很大,它的滞后和崩溃 -
而且当我将平面列表包装在滚动视图中时,onendreached 被称为无限次并且应用程序崩溃并且当我删除滚动视图时 onendreached 被正确调用
标签: react-native react-native-flatlist