“The way to get started is to quit talking and begin doing.” – Walt Disney
Overview
In this article, we will learn about how to load data asynchronously and export JSON data to excel in react using the react-csv package.
What is react-csv?
It is a popular npm package to generate a CSV file and download it from given data. It accepts data like array of arrays, an array of literal objects, or strings.
You can use react-csv by npm (node developers) or CDN directly (non-node developers).
The react-csv package includes two components: CSVLink and CSVDownload. In this article, we are going to use the CSVLink component.
Common Props
Both components CSVLink and CSVDownload accepts the following props:
- data: A required property that represents the CSV data. This data can be an array of array, an array of literal objects, or string. This can also be a function that returns any of these things.
- headers: Specifying headers helps to define an order of the CSV fields. The CSV content will be generated accordingly.
- filename: filename is another prop restricted to CSVLink. It specifies the filename of the downloaded CSV.
Package Installation
Using npm,
npm install react-csv –save
Or for non-node developers, you can use CDN directly:
<script src="https://cdn.rawgit.com/abdennour/react-csv/6424b500/cdn/react-csv-latest.min.js" type="text/javascript"></script>
Project Directory
Create a folder within the src folder and name its components. Within the components, folder create two files, one ExportCSV.js, and second UserTable.js.
So, at this point our project directory tree looks like this.
└───src/
├── App.css
├── App.css
├── index.css
├── index.js
└── components/
├── ExportCSV.js
└── UserTable.js
ExportCSV Component
Here, we will use the JSONPlaceholder dummy API to get the list of users using the GET method of Axios. Add the following code in the ExportCSV.js file.
import React, { useState, useEffect } from 'react';
import { CSVLink } from 'react-csv';
import { Button } from '@material-ui/core';
import UserTable from './UserTable';
const axios = require('axios');
const ExportCSV = () => {
const fileName = "users-detail";
const [userData, setUserData] = useState([]);
const [loading, setLoading] = useState(false);
const headers = [
{ label: "Id", key: "id" },
{ label: "Name", key: "name" },
{ label: "Email", key: "email" },
{ label: "Phone", key: "phone" }
];
useEffect(() => {
getUserData();
}, []);
const getUserData = () => {
setLoading(true);
axios.get('https://jsonplaceholder.typicode.com/users')
.then((res) => {
setUserData(res.data);
setLoading(false);
})
.catch((err) => {
console.log("Error: ", err);
setLoading(false);
})
}
return (
<div className='container'>
<Button
variant="contained"
color="primary"
className='export-btn'
>
<CSVLink
headers={headers}
data={userData}
filename={fileName}
style={{ "textDecoration": "none", "color": "#fff" }}
>
{loading ? 'Loading csv...' : 'Export Data'}
</CSVLink>
</Button>
<UserTable headers={headers} userData={userData} />
</div>
);
}
export default ExportCSV;


Jay PankhaniyaPosted Jan 26, 2022, 3:27 PM
Great Article.