I have an Ionic React app and want to download data as CSV. Therefore I added react-papaparse, but I am unable to download the data with the app. As soon as I try it in the browser, it works fine.
In the end, I want to be able to fetch data stored on the phone, convert it from JSON to CSV, and then download it to the phone. I tried it as well with my fetch data as with a dummy array. Neither worked.
But when I click the button inside the browser, I am able to save the generated CSV file. Therefore I think my basic code is working, I am just missing a piece to make it work on the phone/emulator (android studio).
let data
let filename
class ExportCSV extends React.Component {
getData = async() => {
let {value} = await Storage.get({key: 'csv' })
//data = JSON.stringify(value)
data = value
}
getName = () => {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = dd + '-' + mm + '-' + yyyy;
filename = "damage_" + today //+ ".csv"
}
render() {
this.getName()
const exportCSV = async () => {
await this.getData()
let csv = jsonToCSV(data)
return csv
}
const inputJSON = [
{
Name: "Steve Rogers",
Hero: "Captain America",
Color: "Blue & Red",
Weapon: "Grit & Discipline"
},
{
Name: "Tony Stark",
Hero: "Ironman",
Color: "Red & Gold",
Weapon: "Money & Mind"
},
{
Name: "Dr. Banner",
Hero: "Hulk",
Color: "Green",
Weapon: "Mind & Anger"
},
{
Name: "Dr. Strange",
Hero: "Dr. Strange",
Color: "Red",
Weapon: "Magic"
},
{
Name: "Thor",
Hero: "Thor",
Color: "Multi",
Weapon: "Immortality"
}
];
//<CSVDownloader filename={filename} data={inputJSON} bom={true} download={true}><IonButton>Export CSV</IonButton></CSVDownloader>
return (
<CSVDownloader filename={filename} data={() =>exportCSV()} bom={true} download={true}><IonButton>Export CSV</IonButton></CSVDownloader>
);
}
}
export default ExportCSV