Search code examples
reactjstypescriptexport-to-csv

How to bind React-csv with props which are empty initially?


const props{
    csvData: events[]
}
<CSVLink data={csvData}>Download me</CSVLink>;

I am trying to use react-CSV and export table as CSV but the initial value of the event is empty and CSVLink throwing error of undefined value


Solution

  • One solution would be to conditionally render the CSVLink component once there is csvData available.

    const props{
        csvData: events[]
    }
    if (csvData?.length > 0) {
        return <CSVLink data={csvData}>Download me</CSVLink>;
    }
    return (<p>Waiting for data...</p>)