Skip to main content

Infinite scroll

With infinite, the grid shows every row you pass and asks for more as the user nears the end.

Basic usage​

server-infinite.tsx
import { useCallback, useEffect, useRef, useState } from 'react';
import { DataGrid, type QueryState } from 'expo-flash-datagrid';

import { fetchUsers, initialQuery, userColumns, type User } from './data';

export function ServerInfiniteScreen() {
const [query, setQuery] = useState<QueryState>(initialQuery);
const [rows, setRows] = useState<User[]>([]);
const [rowCount, setRowCount] = useState(0);
const [loading, setLoading] = useState(false);
const cursor = useRef(0);
const requestId = useRef(0);

const load = useCallback(
async (page: number, append: boolean) => {
const id = ++requestId.current;
setLoading(true);

const result = await fetchUsers({
...query,
paginationModel: { ...query.paginationModel, page },
});

if (id !== requestId.current) return;

cursor.current = page;
setRows((previous) =>
append ? [...previous, ...result.rows] : result.rows
);
setRowCount(result.total);
setLoading(false);
},
[query]
);

useEffect(() => {
load(0, false);
}, [load]);

const handleEndReached = useCallback(() => {
const nextPage = cursor.current + 1;
const nextOffset = nextPage * query.paginationModel.pageSize;

if (!loading && nextOffset < rowCount) {
load(nextPage, true);
}
}, [load, loading, query.paginationModel.pageSize, rowCount]);

return (
<DataGrid<User>
mode="server"
infinite
rows={rows}
columns={userColumns}
rowCount={rowCount}
loading={loading}
state={query}
onStateChange={setQuery}
onEndReached={handleEndReached}
onEndReachedThreshold={0.4}
style={{ flex: 1 }}
/>
);
}

Options​

PropTypeDefaultDescription
infinitebooleanfalseTurns off page slicing and enables onEndReached.
onEndReached() => voidNoneCalled near the end of the list. Forwarded only when infinite is set.
onEndReachedThresholdnumber0.5Distance from the end that triggers it, in viewport lengths.

The footer replaces page controls with a count of the loaded rows.

Client vs server​

In client mode, infinite displays all processed rows at once. In server mode you own the cursor: reset it when filters, sort, or search change, and append each new page to rows.

onEndReached can fire more than once. Guard against concurrent requests, as the example does with loading.