I am trying to implement pagination in React Native using FlatList. I have followed the best practices, yet I am still getting the following error:
VirtualizedList: You have a large list that is slow to update - make sure your renderItem function renders components that follow React performance best practices like PureComponent, shouldComponentUpdate, etc. Object { "contentLength": 23651.732421875, "dt": 1394, "prevDt": 865, }
Here is the code:
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);
I have used memo
and moved the renderItem
outside the functional component as suggested by many other posts. Still no luck. Thank you for your help!
UPDATE:
The problem was due to conditionally rendering the ListFooterComponent
(i.e. ListFooterComponent={hasMoreToFetch && <ActivityIndicator animating={isLoadingMore} />}
). Changing it to ListFooterComponent={<ActivityIndicator animating={isLoadingMore} />
solves the issue. An issue has been opened by @parse (refer to the comments below) and can be found here.