react-csv
Generate CSV document according to given data.
This data can be array of arrays , or array of literal objects or string which is the CSV content.
Generate CSV document according to given data.
This data can be array of arrays , or array of literal objects or string which is the CSV content.
Example :
Install
npm install react-csv --save;
or , for non-node developers , you can use CDN directly :
Components:
This package includes actually two components: CSVLink
and CSVDownload
.
0. Common Props:
The two components accepts the following Props
:
- data Props:
a required property that represents the CSV data.
This data can be array of arrays, array of literal objects or string.
.i.e (array of arrays)
.i.e (array of literal objects)
.i.e (string)
- headers Props:
Specifying headers
helps to define an order of CSV fields , so , 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 CSV content.- The meaning of headers with data of type
String
data is only prepending those headers as the first line of CSV content.
Custom Header Labels
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:
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:
- separator Props:
Following a request to add this feature , from 1.0.1
release, react-csv
supports separator
props which is equals by default a comma ,
.
1. CSVLink Component:
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 also , it rendered all props of HTMLAnchor
tag. (className, target,....)
- filename Props:
filename
is another props restricted to CSVLink
. It specifies the filename of the downloaded CSV .
example
- onClick Props:
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
- ? Sync + Proceed
- ? Sync + Don't Proceed
- ? ASync + Proceed
- ? Async + Don't Proceed
2. CSVDownload Component:
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 :
Thus, target
, specs
and replace
Props are available .
example
For non-node developers, they have to use CDN version :