React CSV Downloader
react-csv-downloader is a React component designed for client-side CSV file generation and download directly from JavaScript objects. Currently stable at version 3.3.0, the library is actively maintained with releases as needed, typically every few months, addressing issues and introducing minor enhancements. Its key differentiators include a straightforward API for defining columns and data, robust support for asynchronous data loading via Promises or functions, and extensive options for customizing filename prefixes, suffixes, and column separators. It empowers developers to provide users with direct CSV download capabilities without requiring server-side data processing, making it particularly suitable for front-end heavy applications. The package also ships with comprehensive TypeScript types, enhancing the developer experience by providing strong type checking and autocompletion.
Common errors
-
Failed prop type: The prop `filename` is marked as required in `CsvDownloader`, but its value is `null`.
cause The 'filename' prop was not provided to the CsvDownloader component, or was explicitly set to null/undefined.fixAdd a `filename` prop with a string value: `<CsvDownloader filename="my_data_export" ... />`. -
Module not found: Can't resolve 'react-csv-downloader' in '.../src/App.js'
cause The `react-csv-downloader` package is either not installed or the import path is incorrect.fixFirst, ensure the package is installed by running `npm install --save react-csv-downloader`. Then, verify the import statement is `import CsvDownloader from 'react-csv-downloader';`. -
TypeError: Cannot read properties of undefined (reading 'map') (or similar data processing error in the browser console leading to an empty CSV)
cause The `datas` prop did not resolve to a correctly formatted array of objects, or the array was empty unexpectedly. This often happens with async data if the promise doesn't resolve or resolves with incorrect data.fixDebug the `datas` prop's value. If it's an array, ensure it contains objects. If it's a function, confirm it returns a `Promise` that resolves to an array of objects, and that the resolved data is not empty or malformed. -
React.Children.only expected to receive a single React element child.
cause The `CsvDownloader` component was given multiple children, or a non-element child (e.g., a string or fragment) when used as a wrapper for a trigger element.fixEnsure `CsvDownloader` wraps only a single React element, like a `<button>` or `<a>`. If you don't need to wrap a child, use the `text` prop instead: `<CsvDownloader text="Download" ... />`.
Warnings
- breaking Version 3.0.0 of `react-csv-downloader` included a migration to TypeScript and internal dependency updates. While no explicit API breaks were announced, projects relying on specific internal behaviors or older browser compatibility might experience subtle changes. Thorough testing is recommended when upgrading from v2.x.x.
- gotcha The `filename` prop is marked as required in the component's type definition and documentation, but examples sometimes omit it. Not providing this prop will result in console warnings and may lead to unexpected default filenames or behavior in different browser environments.
- gotcha Ensure your project's `react` version falls within the peer dependency range (`^16.6.3 || ^17.0.0 || ^18.0.0 || ^19.0.0`). Mismatched peer dependencies can lead to installation warnings, unexpected runtime errors, or instability within your React application.
- gotcha When providing the `datas` prop as a function, it must return a `Promise` that resolves to an array of plain JavaScript objects. Incorrectly formatted data (e.g., non-Promise return, malformed array, or non-object items) can result in an empty or corrupt CSV file without explicit error messages from the component.
Install
-
npm install react-csv-downloader -
yarn add react-csv-downloader -
pnpm add react-csv-downloader
Imports
- CsvDownloader
const CsvDownloader = require('react-csv-downloader');import CsvDownloader from 'react-csv-downloader';
- CsvDownloaderProps
import type { CsvDownloaderProps } from 'react-csv-downloader'; - Column
type Column = { id: string; displayName: string; };
Quickstart
import React, { useState } from 'react';
import CsvDownloader from 'react-csv-downloader';
interface DataRow {
id: number;
name: string;
email: string;
}
const columns = [
{ id: 'id', displayName: 'User ID' },
{ id: 'name', displayName: 'Full Name' },
{ id: 'email', displayName: 'Email Address' },
];
const MyCsvExporter: React.FC = () => {
const [loading, setLoading] = useState(false);
// Simulate an async data fetch
const getAsyncData = (): Promise<DataRow[]> => {
setLoading(true);
return new Promise((resolve) => {
setTimeout(() => {
const data: DataRow[] = [
{ id: 1, name: 'John Doe', email: 'john.doe@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane.smith@example.com' },
{ id: 3, name: 'Peter Jones', email: 'peter.jones@example.com' },
];
setLoading(false);
resolve(data);
}, 1500);
});
};
return (
<div>
<h1>User Data Export</h1>
<p>Click the button to download user data as a CSV file.</p>
<CsvDownloader
columns={columns}
datas={getAsyncData} // Using async function resolver
filename="user_data_export"
extension=".csv"
separator=";"
prefix={true} // Add timestamp prefix
>
<button disabled={loading}>
{loading ? 'Preparing...' : 'Download CSV'}
</button>
</CsvDownloader>
<br />
<p>Or a simpler button without children:</p>
<CsvDownloader
datas={[{ name: 'Alice', age: 30 }, { name: 'Bob', age: 24 }]}
filename="simple_data"
text="Download Simple CSV"
/>
</div>
);
};
export default MyCsvExporter;