Gathering detailed insights and metrics for react-csv
Gathering detailed insights and metrics for react-csv
Gathering detailed insights and metrics for react-csv
Gathering detailed insights and metrics for react-csv
@types/react-csv
TypeScript definitions for react-csv
react-csv-downloader
React csv downloader
@ag-grid-community/csv-export
Advanced Data Grid / Data Table supporting Javascript / Typescript / React / Angular / Vue
@handsontable/react
Best Data Grid for React with Spreadsheet Look and Feel.
React components to build CSV files on the fly basing on Array/literal object of data
npm install react-csv
Typescript
Module System
Node Version
NPM Version
JavaScript (99.99%)
HTML (0.01%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1,178 Stars
230 Commits
262 Forks
17 Watchers
26 Branches
28 Contributors
Updated on Jun 30, 2025
Latest Version
2.2.2
Package Id
react-csv@2.2.2
Unpacked Size
39.97 kB
Size
11.52 kB
File Count
20
NPM Version
8.1.2
Node Version
16.13.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
34
Generate a CSV file from given data.
This data can be an array of arrays, an array of literal objects, or strings.
1import { CSVLink, CSVDownload } from "react-csv"; 2 3const csvData = [ 4 ["firstname", "lastname", "email"], 5 ["Ahmed", "Tomi", "ah@smthing.co.com"], 6 ["Raed", "Labes", "rl@smthing.co.com"], 7 ["Yezzi", "Min l3b", "ymin@cocococo.com"] 8]; 9<CSVLink data={csvData}>Download me</CSVLink>; 10// or 11<CSVDownload data={csvData} target="_blank" />;
For more examples, see here 👈🏼
npm install react-csv --save;
Or for non-node developers, you can use CDN directly:
1<script src="https://cdn.rawgit.com/abdennour/react-csv/6424b500/cdn/react-csv-latest.min.js" type="text/javascript"></script>
This package includes two components: CSVLink
and CSVDownload
.
The two components accept the following Props
:
A required property that represents the CSV data. This data can be array of arrays, array of literal objects or string. This can also be a function that returns any of these things.
Example of Array of arrays
1// Array of arrays. Each item is rendered as a CSV line 2data = [ 3 ["firstname", "lastname", "email"], 4 ["Ahmed", "Tomi", "ah@smthing.co.com"], 5 ["Raed", "Labes", "rl@smthing.co.com"], 6 ["Yezzi", "Min l3b", "ymin@cocococo.com"] 7];
Example of array of literal objects
1// Array of literal objects. Each item is rendered as CSV line however the order of fields will be defined by the headers props. If the headers props are not defined, the component will generate headers from each data item. 2data = [ 3 { firstname: "Ahmed", lastname: "Tomi", email: "ah@smthing.co.com" }, 4 { firstname: "Raed", lastname: "Labes", email: "rl@smthing.co.com" }, 5 { firstname: "Yezzi", lastname: "Min l3b", email: "ymin@cocococo.com" } 6];
Example of strings
1// A string can be used if the data is already formatted correctly 2 3data = `firstname,lastname 4Ahmed,Tomi 5Raed,Labes 6Yezzi,Min l3b 7`; 8 9// or using 3rd party package 10import json2csv from "json2csv"; 11data = json2csv(arrayOfLiteralObjects);
Example of function returning data
1// this function just returns a basic array, but you could also map or return some recently downloaded data in state 2function dataFromAsyncProcess() { 3 return [ 4 { firstname: "Ahmed", lastname: "Tomi", email: "ah@smthing.co.com" }, 5 { firstname: "Raed", lastname: "Labes", email: "rl@smthing.co.com" }, 6 { firstname: "Yezzi", lastname: "Min l3b", email: "ymin@cocococo.com" } 7 ]; 8}
Specifying headers
helps to define an order of the CSV fields. The csv content will be generated accordingly.
Notes :
- The meaning of headers with data of type
Array
is to order fields AND prepend those headers at the top of the CSV content.- The meaning of headers with data of type
String
data is only prepending those headers as the first line of the CSV content.
Custom header labels can be used when converting data of type Object
to CSV by having the header array itself be an array of literal objects of the form:
1{ label: /* Label to display at the top of the CSV */, key: /* Key to the data */ }
If the header array is an array of strings, the header labels will be the same as the keys used to index the data objects.
Example:
1import { CSVLink } from "react-csv"; 2 3headers = [ 4 { label: "First Name", key: "firstname" }, 5 { label: "Last Name", key: "lastname" }, 6 { label: "Email", key: "email" } 7]; 8 9data = [ 10 { firstname: "Ahmed", lastname: "Tomi", email: "ah@smthing.co.com" }, 11 { firstname: "Raed", lastname: "Labes", email: "rl@smthing.co.com" }, 12 { firstname: "Yezzi", lastname: "Min l3b", email: "ymin@cocococo.com" } 13]; 14 15<CSVLink data={data} headers={headers}> 16 Download me 17</CSVLink>;
It is possible to reference nested strings in your data using dot notation
1headers = [ 2 { label: 'First Name', key: 'details.firstName' }, 3 { label: 'Last Name', key: 'details.lastName' }, 4 { label: 'Job', key: 'job' }, 5]; 6 7data = [ 8 { details: { firstName: 'Ahmed', lastName: 'Tomi' }, job: 'manager'}, 9 { details: { firstName: 'John', lastName: 'Jones' }, job: 'developer'}, 10];
Note: if at any point the nested keys passed do not exist then looks for key with dot notation in the object.
Following a request to add this feature , from 1.0.1
release, react-csv
supports separator
props which is equals by default a comma ,
.
1import { CSVLink } from "react-csv"; 2 3<CSVLink data={array} separator={";"}> 4 Download me 5</CSVLink> 6 7/* 8 "foo";"bar" 9 "a";"b" 10 */
Following a request to add this feature, react-csv
supports an enclosingCharacter
prop which defaults to "
.
1import {CSVLink} from 'react-csv'; 2 3<CSVLink data={array} enclosingCharacter={`'`}> 4 Download me 5</CSVLink> 6 7/* 8 'foo','bar' 9 'a','b' 10 */ 11
It renders a hyperlink and clicking on it will trigger the download action of the CSV document.
It does not accept only data
and headers
props, but it also renders all props of HTMLAnchor
tag. (className, target,....)
filename
is another props restricted to CSVLink
. It specifies the filename of the downloaded CSV.
example
1import { CSVLink } from "react-csv"; 2 3<CSVLink 4 data={data} 5 filename={"my-file.csv"} 6 className="btn btn-primary" 7 target="_blank" 8> 9 Download me 10</CSVLink>;
onClick
is another props restricted to CSVLink
.
If it is defined, it means 3 things:
1 - It will run at the top of the click handling logic.
2 - [Sync] If it returns an explicit false
, the return will be interpreted as a claim to stop the click handling, then, the next logic will not be executed if so.
3 - [Async] If it is async, "done" argument must be called if you want to invoke the handling of the component. (check examples below)
4 - [Async] If it is async (includes api call, timeout,... ) and it calls done with false
will be interpreted as a claim to stop the click handling, then, the next logic will not be executed if so.
examples
1import { CSVLink } from "react-csv"; 2 3<CSVLink 4 data={data} 5 onClick={() => { 6 console.log("You click the link"); // 👍🏻 Your click handling logic 7 }} 8> 9 Download me 10</CSVLink>;
1import { CSVLink } from "react-csv"; 2 3<CSVLink 4 data={data} 5 onClick={event => { 6 console.log("You click the link"); 7 return false; // 👍🏻 You are stopping the handling of component 8 }} 9> 10 Download me 11</CSVLink>;
1import { CSVLink } from "react-csv"; 2 3<CSVLink 4 data={data} 5 asyncOnClick={true} 6 onClick={(event, done) => { 7 axios.post("/spy/user").then(() => { 8 done(); // REQUIRED to invoke the logic of component 9 }); 10 }} 11> 12 Download me 13</CSVLink>;
1import { CSVLink } from "react-csv"; 2 3<CSVLink 4 data={data} 5 asyncOnClick={true} 6 onClick={(event, done) => { 7 axios.post("/spy/user").then(() => { 8 done(false); // Don't Proceed 9 }); 10 }} 11> 12 Download me 13</CSVLink>;
1import { CSVLink } from "react-csv"; 2 3export default class DownloadUserCSVButton extends React.Component { 4 constructor(props: {}) { 5 super(props); 6 7 this.state = { 8 listOfUsers: [], 9 loading: false 10 }; 11 } 12 13 getUsers = (event, done) => { 14 if(!this.state.loading) { 15 this.setState({ 16 loading: true 17 }); 18 axios.get("/api/users").then((userListJson) => { 19 this.setState({ 20 listOfUsers: userListJson, 21 loading: false 22 }); 23 done(true); // Proceed and get data from dataFromListOfUsersState function 24 }).catch(() => { 25 this.setState({ 26 loading: false 27 }); 28 done(false); 29 }); 30 } 31 } 32 33 dataFromListOfUsersState = () => { 34 return this.state.listOfUsers; 35 } 36 37 render() { 38 const {loading} = this.state; 39 return <CSVLink 40 data={this.dataFromListOfUsersState} 41 asyncOnClick={true} 42 onClick={this.getUsers} 43 > 44 {loading ? 'Loading csv...' : 'Download me'} 45 </CSVLink>; 46 } 47} 48
It triggers downloading ONLY on mounting the component. so , be careful to render this component whenever it is needed.
It does not accept only data
and headers
props , but also , it takes advantage of all arguments of window.open
function known that its signature is :
1window.open(ARG0, target, specs, replace);
Thus, target
, specs
and replace
Props are available .
example
1import { CSVDownload } from "react-csv"; 2 3<CSVDownload data={data} target="_blank" />;
For non-node developers, they have to use CDN version :
1 <script src="https://cdn.rawgit.com/abdennour/react-csv/6424b500/cdn/react-csv-latest.min.js" type="text/javascript"></script> 2 3 <script type="text/babel"> 4 const {CSVDownload, CSVLink} = ReactCSV;// or window.ReactCSV 5 6 const element= (<CSVDownload data={data} target="_blank" />); 7 8 ReactDOM.render(element, document.querySelector('#app')); 9 </script>
Unit-tests must cover at least 90% of code .
Write documentation of the new class, function, method, attribute ..so on.. following JSDoc syntax.
Add an example for the new feature to sample-site
.
docker-compose run --rm npm start
runs the sample-site
docker-compose run --rm npm run docgen
generates documentation in HTML output.
docker-compose run --rm npm run cdn
generate a bundle to be used as CDN
If this project help you reduce time to develop, you can give me a cup of coffee 🍵 :)
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 10/21 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
125 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More