Airtable’s API limits the number of records returned to 100 by default. To retrieve more than this amount, I have implemented an offset mechanism. However, I want to display these records in incremental batches as the user scrolls to the bottom of the list. Whenever the user reaches the end, additional records should be fetched.
Here’s my current approach:
const myApiKey = "MY_API_KEY";
const myBaseId = "MyBaseId";
const myTableName = "MyTable";
const myView = "MyGridView";
const recordsPerPage = 10;
const myBase = new Airtable({ apiKey: myApiKey }).base(myBaseId);
const MainComponent = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
const [nextOffset, setNextOffset] = useState(0);
useEffect(() => {
setLoading(true);
fetchRecords();
}, []);
async function fetchRecords() {
setLoading(true);
const result = await myBase(myTableName)
.select({ view: myView, pageSize: recordsPerPage, offset: nextOffset })
.all();
setData(prevData => [...prevData, ...result]);
if (result.offset) {
setNextOffset(result.offset);
} else {
setNextOffset(0);
}
setLoading(false);
}
const renderDataItem = ({ item }) => {
return <DataCard image={item.fields.imageField} />;
};
const renderLoadingFooter = () => {
return loading ? <LoaderView size="large" /> : null;
};
const handleScrollEnd = () => {
if (!loading && nextOffset !== undefined) {
fetchRecords();
}
};
return (
<View style={styles.wrapper}>
<FlatList
data={data}
renderItem={renderDataItem}
keyExtractor={(item) => item.id}
ListFooterComponent={renderLoadingFooter}
onEndReached={handleScrollEnd}
onEndReachedThreshold={0.5}
/>
</View>
);
};
export default MainComponent;
Despite this implementation, all records seem to load simultaneously instead of in batches. Could someone assist in identifying the issue?