How to extract data to React state from CSV file using Papa Parse?
Asked Answered
F

3

7

I'm using Papa Parse to parse a CSV file for Graphs. I want to store the data in React state after the file is parsed. Papa.Parse() doesn't return anything and results are provided asynchronously to a callback function. Also, setState() doesn't work inside a async callback. This question is similar to Retrieving parsed data from CSV.

I tried storing the data in state using below code, but as expected it didn't work.

componentWillMount() {

    function getData(result) {
      console.log(result); //displays whole data
      this.setState({data: result}); //but gets error here
    }

    function parseData(callBack) {
      var csvFilePath = require("./datasets/Data.csv");
      var Papa = require("papaparse/papaparse.min.js");
      Papa.parse(csvFilePath, {
        header: true,
        download: true,
        skipEmptyLines: true,
        complete: function(results) {
          callBack(results.data);
        }
      });
    }

    parseData(getData);
}

Here's the error I get when I set state inside getData(). enter image description here

The data is usable inside getData(), but I want to extract it.

How should I store the data in state or in some other variable so that I can use it for Graphs?

Fenella answered 20/10, 2017 at 13:3 Comment(6)
Can you rephrase your subject line into a question that is easy to read and understand? Your post doesn't contain a question until the last line, that is not ideal.Elisabetta
@Jacob please check the last line.Fenella
What exactly is the error message you get? (At this.setState({data: result});)Lachrymose
@Amanshu: I meant: the subject of your post needs to be a question. Stack Overflow is based around Questions and Answers. If your post is not titled with a question, how can there be an answer to go with it? (See the menu bar at the top: it says Questions first!)Elisabetta
@Lachrymose question updated.Fenella
@Jacob is it better now?Fenella
L
13

The problem:

You try to call this.setState in the function getData. But this does not exist in the context of this function.

The solution:

I would try to not write function in functions, but write the functions in the class.

You class could look like this:

import React, { Component } from 'react';

class DataParser extends Component {

  constructor(props) {
    // Call super class
    super(props);

    // Bind this to function updateData (This eliminates the error)
    this.updateData = this.updateData.bind(this);
  }

  componentWillMount() {

    // Your parse code, but not seperated in a function
    var csvFilePath = require("./datasets/Data.csv");
    var Papa = require("papaparse/papaparse.min.js");
    Papa.parse(csvFilePath, {
      header: true,
      download: true,
      skipEmptyLines: true,
      // Here this is also available. So we can call our custom class method
      complete: this.updateData
    });
  }

  updateData(result) {
    const data = result.data;
    // Here this is available and we can call this.setState (since it's binded in the constructor)
    this.setState({data: data}); // or shorter ES syntax: this.setState({ data });
  }

  render() {
    // Your render function
    return <div>Data</div>
  }
}

export default DataParser;
Lachrymose answered 20/10, 2017 at 13:44 Comment(2)
@Lachrymose hey guys, I'm trying to recreate something like this solution but require call doesn't work for me.. any ideas? #48800751Surratt
@Larce, why does the bare this.updateData work as the callback function for complete, but this.updateData(result) does not work? If I replace this.updateData with an anonymous function that takes result and console.log's it, it works.Payable
P
1

You need to bind the getData():

function getData(result) {
    console.log(result); // displays whole data
    this.setState({data: result}); // but gets error here
}.bind(this)
Pozzy answered 7/4, 2020 at 10:24 Comment(0)
T
0

You can try react-papaparse for an easy way. For more detail about react-papaparse please visit react-papaparse. Thanks!

Thistly answered 17/1, 2020 at 4:42 Comment(1)
Note that this was posted by the creator of react-papaparse.Granada

© 2022 - 2024 — McMap. All rights reserved.