Complete the code to call the function when the list reaches the end.
import React from 'react'; import { FlatList } from 'react-native'; export default function MyList() { const loadMore = () => { console.log('Load more items'); }; return ( <FlatList data={[]} renderItem={() => null} onEndReached=[1] /> ); }
The onEndReached prop expects a function reference, so you pass loadMore without parentheses.
Complete the code to set the threshold for triggering onEndReached when the user scrolls near the bottom.
<FlatList
data={items}
renderItem={renderItem}
onEndReached={loadMore}
onEndReachedThreshold=[1]
/>The onEndReachedThreshold expects a decimal between 0 and 1 representing how far from the bottom the event triggers. 0.5 means halfway.
Fix the error in the code to prevent multiple calls when reaching the end.
const [loading, setLoading] = React.useState(false);
const loadMore = () => {
if ([1]) return;
setLoading(true);
fetchMoreItems().then(() => setLoading(false));
};Check if loading is true to avoid starting another load while one is in progress.
Fill both blanks to correctly update the list with new items and stop loading.
const [items, setItems] = React.useState([]);
const [loading, setLoading] = React.useState(false);
const loadMore = () => {
if (loading) return;
setLoading(true);
fetchMoreItems().then(newItems => {
setItems([1]);
setLoading([2]);
});
};Use a function to append new items to the existing list and set loading to false after loading finishes.
Fill all three blanks to create a FlatList with infinite scrolling, loading indicator, and proper key extraction.
import React from 'react'; import { FlatList, ActivityIndicator, View, Text } from 'react-native'; export default function InfiniteList({ data, loadMore, loading }) { return ( <FlatList data={data} keyExtractor={item => item.[1] renderItem={({ item }) => <Text>{item.name}</Text>} onEndReached={loadMore} onEndReachedThreshold={0.5} ListFooterComponent={() => ( loading ? <[2] size="large" color="#0000ff" /> : <[3] /> )} /> ); }
Use id as the key, show ActivityIndicator when loading, and an empty View when not loading.