Airtable only gives 100 records by default on API calls to load more than 100 records I have used offset address but when all the records are called I want to show them in batches when a user scrolls to the end more records should get loaded.
What I have tried …
const apiKey = "API_KEY";
const baseId = "BaseId";
const tableName = "Table1";
const viewName = "Grid view";
const pageSize = 10;
const base = new Airtable({ apiKey }).base(baseId);
const Home = () => {
const [records, setRecords] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [offset, setOffset] = useState(0);
useEffect(() => {
setIsLoading(true);
loadRecords();
}, []);
async function loadRecords() {
setIsLoading(true);
const response = await base(tableName)
.select({ view: viewName, pageSize, offset })
.all();
const newRecords = [...records, ...response];
setRecords(newRecords);
if (response.offset) {
setOffset(response.offset);
} else {
setOffset(0);
}
setIsLoading(false);
console.log("Loaded records:", response);
}
function renderRecord({ item }) {
return <CardView img={item.fields.wallpaper} />;
}
function renderFooter() {
return isLoading ? (
<View style={styles.loader}>
<ActivityIndicator size="large" />
</View>
) : null;
}
function handleLoadMore() {
if (!isLoading && offset !== undefined) {
loadRecords();
}
}
return (
<View style={styles.container}>
<FlatList
data={records}
renderItem={renderRecord}
numColumns={numColumns}
keyExtractor={(item) => item.id}
ListFooterComponent={renderFooter}
onEndReached={handleLoadMore}
onEndReachedThreshold={0}
initialNumToRender={pageSize}
/>
</View>
);
};
export default Home;
With this code all records are loading at once not in batches can somebody help me where am I going wrong
2
Answers
you can use the following props to help you archive what you want
to let
flatlist
render some amount of component every tile while the user is scrolling you can usemaxToRenderPerBatch
you can read this article to help you Optimizing your
flatlist
: https://reactnative.dev/docs/optimizing-flatlist-configurationthe pageSize is dynamically set as records.length + 10 on each load to fetch more records. The offset is not used in this case. Instead, the records.length serves as the starting index for fetching new records. The isFetchingMore state is used to prevent multiple simultaneous fetch requests.
When the user scrolls to the end of the list, the handleLoadMore function is called, which triggers the loadRecords function to fetch more records based on the increased pageSize. The renderFooter function displays an activity indicator when new records are being fetched.
Hope this will help!!