I am trying to add an option to download data as a CSV file inside my component. However, the below code sometimes prints a garbage character  and works other times. I don't understand what I am doing wrong here. I tried to follow the example provided here: https://github.com/react-csv/react-csv#readme
Here is my code
import React, { useState } from "react";
import axios from "axios";
import {
Card,
CardHeader,
CardTitle,
DropdownItem,
DropdownMenu,
DropdownToggle,
Table,
UncontrolledDropdown,
Progress
} from "reactstrap";
import { MoreHorizontal } from "react-feather";
import { CSVLink } from "react-csv";
const Languages = () => {
const [data, setData] = useState([]);
return (
<Card className="flex-fill w-100">
<CardHeader>
<div className="card-actions float-right">
<UncontrolledDropdown>
<DropdownToggle tag="a">
<MoreHorizontal />
</DropdownToggle>
<DropdownMenu right>
<DropdownItem><CSVLink data={data} filename={'my-file.csv'} asyncOnClick={true}
onClick={(event, done) => {
axios.get("http://localhost:9091/api/returnIndices", {
params: {
view: 'Series',
bucket: '3_7',
group: 'Ratings',
metric: 'Price Return',
download: true
}
}).then((response) => {
const data = response.data.payload;
console.log(data);
setData(JSON.parse(data));
done(false); // REQUIRED to invoke the logic of component
});
}}>Download me</CSVLink></DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</div>
<CardTitle tag="h5" className="mb-0">
Languages
</CardTitle>
</CardHeader>
</Card>
);
}
export default Languages;