gpt4 book ai didi

react-native - React Native中FlatList的性能问题

转载 作者:行者123 更新时间:2023-12-03 07:47:05 25 4
gpt4 key购买 nike

我尝试过 Flatlist,但它在 Android 中存在一些性能问题。

  1. 当我向下滚动时,它会加载列表。但之后,向上滚动时显示空白。

  2. 到达屏幕末尾后,会停一会儿,然后加载数据。为什么底部不显示加载程序(事件指示器)?为什么 onEndReached 和 onEndReachedThreshold 不起作用?

请观看视频here

https://youtu.be/5tkkEAUEAHM

我的代码:

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
FlatList,
ActivityIndicator,
} from 'react-native';
import { List, ListItem, SearchBar } from "react-native-elements";

export default class FlatListExample extends Component
{
constructor(props) {
super(props);

this.state = {
loading: false,
data: [],
page: 1,
seed: 1,
error: null,
refreshing: false,
};
}

componentDidMount() {
this.makeRemoteRequest();
}

makeRemoteRequest = () => {
const { page, seed } = this.state;
const url = `https://randomuser.me/api/?seed=${seed}&page=${page}&results=20`;
console.log('url', url);
this.setState({ loading: true });

setTimeout(()=>{
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: [...this.state.data, ...res.results],
error: res.error || null,
loading: false,
refreshing: false
});
})
.catch(error => {
this.setState({ error, loading: false });
});
},0);

};

renderFooter = () => {
if (!this.state.loading) return null;

return (
<View
style={{
paddingVertical: 20,
borderTopWidth: 1,
borderColor: "#CED0CE"
}}
>
<ActivityIndicator animating size="large" />
</View>
);
};

handleLoadMore = () =>{
this.setState({
page:this.state.page + 1,
},()=>{
this.makeRemoteRequest();
})
}
render() {
return (
<FlatList
data={this.state.data}
renderItem={({ item }) => (
<ListItem
roundAvatar
title={`${item.name.first} ${item.name.last}`}
subtitle={item.email}
avatar={{ uri: item.picture.thumbnail }}
/>
)}
keyExtractor={item => item.email}
ListFooterComponent={this.renderFooter}
onEndReached={this.handleLoadMore}
onEndReachedThreshold={50}
/>
);
}
}

AppRegistry.registerComponent('FlatListExample', () => FlatListExample);

最佳答案

我注意到您没有设置initialNumToRender。来自文档:

initialNumToRender: number

How many items to render in the initial batch. This should be enough to fill the screen but not much more. Note these items will never be unmounted as part of the windowed rendering in order to improve perceived performance of scroll-to-top actions.

因此,您需要估计在任何给定时间预计有多少个单元格可见,并将其设置为该值。如果您还没有更新到最新的react-native,我还建议您更新到最新的react-native,其中包括对FlatList组件的各种改进。

关于react-native - React Native中FlatList的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44230862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com