I use Para Parse to analyze the CSV file for graphs. I want to save the data in React state after the file is parsed. Papa.Parse () returns nothing, and the results are provided asynchronously to the callback function. In addition, setState () does not work inside an asynchronous callback. This question is similar to Getting Analyzed Data from CSV .
I tried to keep the data in a state using the code below, but as expected, it did not work.
componentWillMount() {
function getData(result) {
console.log(result);
this.setState({data: result});
}
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);
}
This is where the error occurs when I set the state inside getData ().

Data can be used inside getData (), but I want to extract it.
- , Graphs?