我正在尝试使用 FlatList 在 React Native 中实现分页。我遵循了最佳实践,但我仍然收到以下错误:
VirtualizedList:您有一个更新缓慢的大型列表 - 确保您的 renderItem 函数呈现遵循 React 性能最佳实践的组件,如 PureComponent、shouldComponentUpdate 等。 Object { "contentLength": 23651.732421875, "dt": 1394, "prevDt" : 865, }
这是代码:
const NewsScreen = ({ isLoading, news, fetchInitialNews, fetchMoreNews, isLoadingMore, hasMoreToFetch }) => {
useEffect(() => {
fetchInitialNews();
}, []);
const onEndReached = () => {
fetchMoreNews();
};
return (
<NewsList
isLoading={isLoading}
news={news}
numSkeletonsToShow={LATEST_NEWS_CONSTANTS.NUM_TO_SHOW}
contentContainerStyle={STYLES.newsListContentContainer}
onEndReached={onEndReached}
isLoadingMore={isLoadingMore}
hasMoreToFetch={hasMoreToFetch}
/>
);
};
const renderNewsItem = ({ item, index }) => (
<NewsItem news={item} containerStyle={index !== 0 ? GLOBAL_STYLES.cardMargin : null} />
);
const NewsList = ({
isLoading,
news = [],
isLoadingMore,
contentContainerStyle = {},
onEndReached,
hasMoreToFetch
}) => {
const dummySkeletonArray = Array(numSkeletonsToShow).fill("1");
const onScrollToEnd = () => {
if (!isLoadingMore && hasMoreToFetch) {
onEndReached();
}
};
if (isLoading) {
return (
//..loading indicator
);
}
return (
<FlatList
data={news}
keyExtractor={(n) => n.url}
renderItem={renderNewsItem}
showsVerticalScrollIndicator={false}
style={GLOBAL_STYLES.flatListContentContainer}
contentContainerStyle={contentContainerStyle}
onEndReached={onScrollToEnd}
onEndReachedThreshold={0.2}
ListFooterComponent={hasMoreToFetch && <ActivityIndicator animating={isLoadingMore} />}
/>
);
};
const areEqual = () => true;
const NewsItem = ({ news, containerStyle }) => {
return (
<TouchableNativeFeedback viewContainerStyle={containerStyle}>
<Card>
</Card>
</TouchableNativeFeedback>
);
};
export default memo(NewsItem, areEqual);
正如许多其他帖子所建议的那样,我已经使用memo
并移动了renderItem
功能组件的外部。仍然没有运气。谢谢您的帮助!
更新:
问题是由于有条件地渲染ListFooterComponent
(即ListFooterComponent={hasMoreToFetch && <ActivityIndicator animating={isLoadingMore} />}
)。更改它以ListFooterComponent={<ActivityIndicator animating={isLoadingMore} />
解决问题。@parse 已经打开了一个问题(请参阅下面的评论),可以在这里找到。