Does reader.onload function have an event trigger?

I've been cracking this for like 6 hours and still I can't find a solution for this. I want to read a csv file with ReactFileReader and DataParser. The problem is that "reader.onload" enters the function but then somehow the data that I'm trying to copy from "data" into "this.state.data" doesn't properly copy. With some proper console logs I get that "data" is undefined or null when executing.
Is there an event trigger for this function to properly work? Or, why this isn't working as expected?
Here's the code from the class that I've been working:
import React from 'react';
import Button from '@material-ui/core/Button';
import PropTypes from 'prop-types';
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
import DataParser from './DataParser.jsx';
import ReactFileReader from 'react-file-reader';

class FromFileButton extends React.Component{
    constructor(props){
            super(props);
    }

    state = {
            data: null
    }

    setState = (value) =>
    {
            this.state.data=value;
    };

    handleFiles = (files) => {
            let tempData;
            var reader = new FileReader();
            reader.onload = function(e) {
                    let data;
                    // Use reader.result
                    //alert(reader.result)
                    data = DataParser.loadDataFromFile(reader.result);
                    this.state.data = data;
                    console.log(this.state.data);
            }
            reader.readAsText(files[0]);
    }
    render(){
            return(
                    <ReactFileReader handleFiles={this.handleFiles} fileTypes={'.csv'}>
                            <Button size="small" color="secondary" variant="outlined"
                                    onClick={() => {this.props.myData(this.state)}}>
                                    Search CSV file</Button>
                    </ReactFileReader>

            )
    }

}

FromFileButton.propTypes = { 
        classes: PropTypes.object.isRequired,
        myData: PropTypes.func
};
export default FromFileButton;
Thanks in advance.

Комментарии

Популярные сообщения из этого блога

Unable to construct Application instance [duplicate]

What is considered 'insulting a Muslim'? And is it a sin? If so, is it a minor or major scene?