React CSV Downloader

3.3.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `CsvDownloader` with asynchronous data fetching, custom columns, filename options, and both child component and `text` prop usage.

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;

view raw JSON →