react-csv icon indicating copy to clipboard operation
react-csv copied to clipboard

2.0.1 do not generate csv data

Open Krasnopir opened this issue 6 years ago • 18 comments

after update 1.1.2 to 2.0.1 saved files are empty. header(first row) is present, but no data. after rollback to 1.1.2 all working fine

Krasnopir avatar Feb 14 '20 19:02 Krasnopir

thank you for reporting the issue. I am looking into it.

abdennour avatar Feb 15 '20 04:02 abdennour

@Krasnopir I tried to reproduce the issue, by running the example website docker-compose up -d .. then, visit http://localhost:8008 .. I exported the CSV file and no issue.

Could you please give more details ? like snippet of data you are exporting.. so on.

abdennour avatar Feb 15 '20 08:02 abdennour

Hi! i also tried simple mock case, and it work. But my case, is maybe strange, i need to get async data after button click. and 1.1.2 worked) https://codesandbox.io/s/green-sea-mew7u?fontsize=14&hidenavigation=1&theme=dark

maybe is there some better idea how can i do it. Thank!

Krasnopir avatar Feb 15 '20 09:02 Krasnopir

i find async examples at doc. And also will try to use CSVDownload from package.

Krasnopir avatar Feb 15 '20 09:02 Krasnopir

there some troubles to get data from out of async function (ex. axios). I need set it to component state. but useState are sync and there no way add callback, only useEffect, but it is not good solution, because generated data can be the same multiple times, and nothing will not triggered download. and i think it is overhead. The best solution is to use done(data) and not use data={data} for async cases

Also CSVDownload i can not use. After data generation i mount a component. But after file downloading, there no way to trigger download more time. As solution can be add callback after file download window open. (ex. onDownloaded) and this callback can trigger clear data in component state, and next click will generate new data and mount CSVDownload again

Krasnopir avatar Feb 15 '20 10:02 Krasnopir

I am feeling that if we merge this PR #179, the issue will be fixed. Could you please check with me and confirm ? If so, we will need help from @djently to resolve the PR conflicts

abdennour avatar Feb 15 '20 15:02 abdennour

The safest way to get rid of deprecated lifecycle methods is to convert the implementation to functions/hooks.

Would recommend doing as such, and bumping up to v3.

For others who are looking for a solution, you can also use this dependency to achieve the same result using regular Javascript (no react):

https://github.com/silverwind/save-csv

avindra avatar Feb 24 '20 17:02 avindra

@avindra thanks. i try to use save-csv in my react app

import saveCsv from 'save-csv';

    onCompleted: data => {
        saveCsv(
            mapData(data?.trackFlatFilteredList?.items),
            {
                filename: `export-${getCurrentDateTime()}.csv`,
                sep: ';',
            }
        );
    },

all is ok!

Krasnopir avatar Feb 25 '20 08:02 Krasnopir

It seems that if I mutate the data in the render at all the csv data is invalid. If I give it static data it generates the csv correctly.

ppoletti avatar Feb 28 '20 16:02 ppoletti

I have the same question in my project, first time I download .csv file, the data is null, the second time csv data updated, but next is the second time data, and I check props data is my current fetch data, it seems like the link is not update

DreFalPoetry avatar Mar 02 '20 08:03 DreFalPoetry

I'm having the same issue, reverted back to previous version for now

MrDavid5465 avatar Mar 02 '20 14:03 MrDavid5465

Hello guys, if you have a fix for this bug, please open PR. And reach me here http://in.abdennoor.com and i will review it immediately.

abdennour avatar Mar 02 '20 14:03 abdennour

@mccabemj we need your help

abdennour avatar Mar 02 '20 14:03 abdennour

having the same issue

nevin22 avatar Mar 09 '20 06:03 nevin22

another option to use export-to-csv package that enable to customize first row - header unlike saveCsv and also work whith async data

import { ExportToCsv } from 'export-to-csv';

    onCompleted: data => {
        const csvExporter = new ExportToCsv({
            fieldSeparator: ';',
            showLabels: true,
            filename: `export-${getCurrentDateTime()}.csv`,
            useKeysAsHeaders: false,
            headers: getHeader(),
        });
        csvExporter.generateCsv(mapData(data?.items));
    },

Krasnopir avatar Mar 15 '20 15:03 Krasnopir

This issue is likely fixed by https://github.com/react-csv/react-csv/pull/211 2.0.1 isn't handling property changes correctly.

mitchell-bu avatar Mar 23 '20 16:03 mitchell-bu

Thank you guys, Thank you @mitchell-bu
🎉🎉🎉🎉🎉🎉🎉 It is now merged and the new version is 2.0.2

https://github.com/react-csv/react-csv/releases/tag/v2.0.2

abdennour avatar Apr 01 '20 10:04 abdennour

@mitchell-bu for subsequent issues, please reach me on linkedin as I got notified immediately there: http://in.abdennoor.com

abdennour avatar Apr 01 '20 10:04 abdennour