Skip to main content

Efficient Data Export in React: Handling Large Data Volumes with Pagination

Photo by Campaign Creators on Unsplash


When dealing with applications that handle large datasets, one common challenge is exporting data into a manageable format, such as Excel, without overwhelming system resources. Exporting hundreds of thousands, or even millions, of records all at once can cause performance issues, timeouts, or even system crashes.

In this guide, we will explore a React approach for efficiently exporting large data volumes using pagination and creating Excel files in batches, ensuring that we avoid bottlenecks and HTTP 500 errors. The solution involves fetching data incrementally and appending it to an Excel file, thus keeping system memory usage in check.

The Problem with Large Data Exports

When exporting large datasets, the traditional approach is to fetch all data at once and export it in one go. However, as the size of the data grows, fetching all records in one request becomes inefficient and risky:

  • Memory limitations: Large datasets can cause memory overloads in both frontend and backend systems.
  • Timeouts: HTTP requests that take too long may result in server timeouts.
  • User experience: Long waits for data export might frustrate users if the process is not properly optimized.

By using pagination, we can load a subset of the data at a time, process it, and append it to an Excel file in batches.

The Solution: Batched Data Export

Here’s the React code that solves this issue by fetching the data page by page and writing it to an Excel file incrementally:

import React, { useState } from 'react';
import axios from 'axios';
import * as XLSX from 'xlsx';

const ExportTransactions = () => {
const [loading, setLoading] = useState(false);
const pageSize = 1000; // Number of records per page

// Function to fetch paginated transactions using axios
const fetchTransactions = async (page) => {
try {
const response = await axios.get(`/api/transactions`, {
params: {
page: page,
size: pageSize,
},
});
return response.data;
} catch (error) {
console.error('Failed to fetch transactions', error);
throw error;
}
};

// Function to export transactions to Excel, appending after each fetch
const exportToExcel = async () => {
setLoading(true);
let currentPage = 1;
let hasMoreData = true;
try {
// Create a new workbook and an empty worksheet
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.aoa_to_sheet([]); // Start with an empty worksheet

// Fetch paginated data and append to worksheet after each fetch
while (hasMoreData) {
const data = await fetchTransactions(currentPage);
const transactions = data.items; // Assuming data.items contains the transaction records

if (transactions.length === 0) {
hasMoreData = false;
break;
}

// Convert the data to an array of arrays (if needed) and append it to the worksheet
const transactionRows = XLSX.utils.json_to_sheet(transactions, { skipHeader: currentPage !== 1 });
XLSX.utils.sheet_add_json(worksheet, transactions, { origin: -1, skipHeader: currentPage !== 1 });

hasMoreData = transactions.length === pageSize;
currentPage++;
}

// Append the worksheet to the workbook and download the Excel file
XLSX.utils.book_append_sheet(workbook, worksheet, 'Transactions');
XLSX.writeFile(workbook, 'Transactions.xlsx');

setLoading(false);
} catch (error) {
console.error('Error exporting transactions:', error);
setLoading(false);
}
};

return (
<div>
<button onClick={exportToExcel} disabled={loading}>
{loading ? 'Exporting...' : 'Export Transactions'}
</button>
</div>
);
};

export default ExportTransactions;

How the Solution Works

The code breaks down the process into manageable steps, allowing you to export large datasets incrementally:

1. Pagination Fetching: The fetchTransactions function is responsible for fetching a specific page of data, determined by the page parameter. By limiting each request to a manageable number of records (1,000 in this example), we avoid overwhelming the system.

2. Batched Export: In the exportToExcel function, the code fetches transactions page by page, starting at page 1. For each page of data:

  • The transactions are appended to the existing Excel sheet.
  • If there are no more records (i.e., the length of transactions is less than the pageSize), the loop exits.

3. Excel File Creation: The XLSX.utils.sheet_add_json method is used to convert the fetched transaction data into a format that can be written to an Excel file. The workbook is constructed incrementally with every page of data. Once all pages are processed, the XLSX.writeFile method triggers the download of the final Excel file, containing all transactions.

4. Efficiency Gains: By exporting data in batches, we avoid system crashes and slow responses caused by large data volumes. The frontend remains responsive, and the risk of server timeouts is greatly reduced.

Benefits of Batched Data Export

This approach offers several benefits:

  • Resource Management: By fetching a limited amount of data per request, we prevent memory overload and avoid large payloads.
  • Improved User Experience: Users can export large datasets without long delays or errors.
  • Scalability: This solution can be easily adjusted by changing the page size, making it suitable for systems of different scales.

Conclusion

Handling large data volumes requires careful consideration to prevent performance bottlenecks. The pagination and batching strategy presented in this React example ensures efficient data export without overwhelming your system or frustrating your users. This approach can be applied to other scenarios as well, such as PDF generation or CSV exports.

By exporting data incrementally, you maintain performance and ensure scalability, even as your data grows.

If you’re not using React, don’t worry — the same principles apply. You can adopt this approach in any environment or framework, whether it’s Angular, Vue.js, or even server-side languages like Node.js or Python. The key takeaway is the concept of fetching data in smaller, manageable chunks and appending it to your export file in batches, ensuring an efficient and scalable solution regardless of the tools you use.

Comments

Popular posts from this blog

Understanding Number Systems: Decimal, Binary, and Hexadecimal

In everyday life, we use numbers all the time, whether for counting, telling time, or handling money. The number system we’re most familiar with is the   decimal system , but computers use other systems, such as   binary   and   hexadecimal . Let’s break down these number systems to understand how they work. What is a Number System? A number system is a way of representing numbers using a set of symbols and rules. The most common number systems are: Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16) Each system has a different “base” that tells us how many unique digits (symbols) are used to represent numbers. Decimal Number System (Base 10) This is the system we use daily. It has  10 digits , ranging from  0 to 9 . Example: The number  529  in decimal means: 5 × 1⁰² + 2 × 1⁰¹ + 9 × 1⁰⁰ =  500 + 20 + 9 = 529 Each position represents a power of 10, starting from the rightmost digit. Why Base 10? Decimal is base 10 because it has 10 digits...

How to Monetize Your API as an Individual Developer While Hosting on Your Own Server?

In the API economy, cloud services like AWS, Google Cloud, and Azure offer many conveniences, such as scaling and infrastructure management. However, some developers prefer more control and autonomy, opting to host their APIs on personal servers. Whether for cost efficiency, data privacy, or customization, hosting your own API comes with both advantages and challenges. But, even without cloud platforms, there are effective ways to monetize your API. This guide will explore how individual developers can successfully monetize their APIs while hosting them on their own servers. Why Host Your API on Your Own Server? Hosting your own API gives you full control over the infrastructure and potentially lower long-term costs. Here’s why some developers choose this approach: Cost Control : Instead of paying ongoing cloud fees, you may opt for a one-time or lower-cost hosting solution that fits your budget and resource needs. Data Ownership : You have full control over data, which is critical if ...

API Testing with Jest and Supertest: A Step-by-Step Guide

API testing is essential to ensure your endpoints behave as expected across all scenarios. In this guide, we’ll explore how to use Jest and Supertest to test a sample API with various response types, including success, authentication errors, and validation errors. By the end, you’ll understand how to apply these tools to check for different response structures and status codes. 0. Prerequisites: Setting Up Your Environment Before diving into API testing, it’s important to ensure that your development environment is properly set up. Here’s what you need to do: Step 1: Install Node.js and npm Node.js  is a JavaScript runtime that allows you to run JavaScript code on the server side. It comes with  npm  (Node Package Manager), which helps you install and manage packages. Installation Steps: Download and install Node.js from the  official website . To verify the installation, open your terminal and run: node -v npm -v This should display the installed versions of Node.js...

The Weight of Responsibility: A Developer’s Journey to Balance Passion and Reality

For the past several years, Eddie has been on a steady climb in his career as a developer, but recently, he found himself at a crossroads — caught between the weight of his responsibilities and the desire to pursue his true passions. His journey began with a three-month internship as a web developer, which led to nearly four years in an application developer role. After that, he spent almost a year as a systems associate, managing tasks across systems analysis, quality assurance, and business analysis. Eventually, he returned to full-time software development for another two years before transitioning into more complex roles. For over a year, he worked as a multi-role software developer and database administrator before stepping into his current position as a senior software developer, database administrator, and cloud administrator — occasionally handling security tasks as well. Now, with over 8 years of professional experience, he also leads a small team of developers, which has been...

Avoiding Confusion in API Design: The Importance of Clear Responses

In today’s fast-paced software development landscape, APIs play a crucial role in connecting services and enabling functionality. However, poor design choices can lead to confusion and inefficiency for both developers and users. One such choice is the omission of a response body for successful requests, a practice I recently encountered in an enterprise API designed for bill payments. The Case of the No-Response API The API in question serves two main endpoints: one for inquiring about account validity and another for confirming payment. When successful, the API returned a  200 OK  status but no response body. This design choice led to significant confusion during our integration process. Even the internal team who developed the said API struggled to justify this approach, revealing a lack of clarity around the rationale behind it. Pros of This Design Choice While the intention behind this design may have been to streamline responses, several potential benefits can be identifi...