Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onFileLoaded doesnt return data for second time #45

Open
hungdev opened this issue Jun 23, 2020 · 9 comments
Open

onFileLoaded doesnt return data for second time #45

hungdev opened this issue Jun 23, 2020 · 9 comments

Comments

@hungdev
Copy link

hungdev commented Jun 23, 2020

Describe the bug and Reproduce
If the file loads the first time, the file has a duplicate header, it calls the onFileLoaded function, but the second load is the same file, it will not call the onFileLoaded function anymore.

Expected behavior
2nd time it still has to run into function onFileLoaded

Screenshots
If applicable, add screenshots to help explain your problem.

Platform (please complete the following information):

  • Browser Chrome
  • Version "react-csv-reader": "^3.0.6"

CSV file: https://drive.google.com/file/d/1QfDRRAzsHZ6P4IiXNq-pUNba2KkOQd3u/view?usp=sharing

My code:

const papaParseOptions = {
  header: true,
  dynamicTyping: true,
  skipEmptyLines: true,
  transformHeader(header) {
    return header
  }
};
        <CSVReader
              onFileLoaded={handleForce}
              parserOptions={papaParseOptions}
            />


  const handleForce = (data, fileInfo) => {
    console.log('data', data)
  };
@hungdev
Copy link
Author

hungdev commented Jun 24, 2020

I found a way to fix it, use if else to check in onFileLoaded func, and clearing data:

(document.getElementById('CSVReader') as HTMLInputElement).value = '';

and attach id to component: inputId='CSVReader' like this:

<CSVReader
              inputId='CSVReader'
              inputStyle={{ display: 'none' }}
              onFileLoaded={handleForce}
              parserOptions={papaParseOptions}
            />

But I think its bad way. I hope we can use better solution. Because i think we shouldnt use pure js ( document.getElementById ) in ReactJs.

@niladri407
Copy link

Same issue is with me. It's working properly in firefox but in chrome and edge It's not reading the file from second time.

@RWTloopesko
Copy link

I'm having the same issue in Chrome. hungdev's solution works well but it seems like there should be a more "Reactish" way to do this. There should be a method to clear the input after it loads.

@csmartinsfct
Copy link

To fix it you can also use a new key in the component every time you load a new file.

@nzambello
Copy link
Owner

I see that it's happening on Chrome and Safari and not on Firefox.
I'm looking at the difference

@danielperaza
Copy link

I have the same issue

@cvanputt
Copy link

Has there been any resolution to this issue?

@adebayoileri
Copy link

I'm having the same issue in Chrome. hungdev's solution works well but it seems like there should be a more "Reactish" way to do this. There should be a method to clear the input after it loads.

Seems the issue still persists, a "react" way could be the useRef

import { useRef } from 'react';

const csvRef = useRef();
<CSVReader
      inputId='CSVReader'
      inputStyle={{ display: 'none' }}
      onFileLoaded={(data) => {
         handleForce(data);
         csvRef.current.value = '';
      }}
      parserOptions={papaParseOptions}
      inputRef={csvRef}
  />

@GisliBG
Copy link

GisliBG commented Sep 14, 2023

If you are looking for a work around, what @csmartinsfct works. Add a key prop to the component and make sure to create a new key after each time onFileLoaded executes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants