I'm using CSV Link from react-csv to export CSV data in a table with selectable rows. There is a problem in the way the data is getting exported. When I select 3 rows, for example, it's omitting the last row from the resulting CSV file that is outputted. If I select 4 rows, it will only export 3 rows the first time, and so on. If I hit the export button a second time, the complete set of rows will get exported.
Here is how I'm passing data to the CSV Link component:
<CSVLink
data={getExportTransactionRows(t.transactions)}
headers={getExportTransactionHeaders(t)}
filename={t.companyName}
>
Here is the getExportTransactionRows
function that returns the rows:
const getExportTransactionRows = (transactions) => {
let trans = transactions.filter((t) => t.selected === true);
if (trans?.length === 0) trans = transactions;
return trans;
};
CodePudding user response:
I think the reason why you just keep exporting the previous row is that your getExportTransactionRows(t.transactions)s don't change value right away when you selected row. You can declare a variable that saves your export data. Then in your select event and change that variable when you selected. Passing it into CSVLink