Pagination is a critical aspect of any application that handles large datasets. It affects both the user experience and the performance of the app, especially when dealing with significant amounts of data.
Typically, there are two main types of pagination:
To strike a balance between these two methods, we can adopt a hybrid pagination strategy. The idea is to:
This way, we reduce the number of network calls while keeping memory usage low and the initial load time quick.
Here’s an example code in React using this strategy:
const SS_PAGESIZE = 25; // Items per page
const SS_BATCHSIZE = 8; // Number of pages per batch (8 pages * 25 records)
const TimeTable = ({ initRecords, initOptions, season }) => {
const [options, setOptions] = useState(initOptions);
const [allRecords, setAllRecords] = useState(initRecords);
const [activePage, setActivePage] = useState(1);
const [batchStart, setBatchStart] = useState(0);
const [isLoading, setIsLoading] = useState(false);
const fetchRecords = async (batchStart) => {
setIsLoading(true);
try {
const tResult = await getRankings(
options.event,
options.type,
options.ageGroup,
options.gender,
season,
batchStart,
SS_PAGESIZE * SS_BATCHSIZE // Fetch a batch of 200 records
);
setAllRecords(tResult);
} catch (err) {
console.error(err);
}
setIsLoading(false);
};
const handlePaginationChange = async (value) => {
const batchStartNow = Math.floor((value - 1) / SS_BATCHSIZE) * SS_PAGESIZE * SS_BATCHSIZE;
if (batchStartNow !== batchStart) {
setBatchStart(batchStartNow);
await fetchRecords(batchStartNow); // Fetch new batch if needed
}
setActivePage(value); // Update the page on the client-side
};
useEffect(() => {
fetchRecords(batchStart);
}, [batchStart, season, options]);
const currentRecords = allRecords.data.slice(
(activePage - 1) % SS_BATCHSIZE * SS_PAGESIZE,
(activePage % SS_BATCHSIZE) * SS_PAGESIZE
);
return (
<>
<Table>
<Thead>
<Tr>
<Th>Name</Th>
<Th>Age</Th>
<Th>Team</Th>
{/* More headers... */}
</Tr>
</Thead>
<Tbody>
{currentRecords.map((record, idx) => (
<Tr key={idx}>
<Td>{record.name}</Td>
<Td>{record.age}</Td>
<Td>{record.team}</Td>
{/* More data... */}
</Tr>
))}
</Tbody>
</Table>
<DNXPagination
activePage={activePage}
totalPages={Math.ceil(allRecords.total / SS_PAGESIZE)}
onPageChange={handlePaginationChange}
/>
</>
);
};
By adopting a hybrid pagination approach, you can drastically reduce the number of network calls while improving the user experience. The app will load faster initially, and the server won't be overwhelmed with too many requests as the user navigates through the data.
This structure combines the theory behind hybrid pagination with the practical React implementation, making it a valuable resource for others interested in improving both performance and UX.