I am new to javascript and reactJS and I'm stuck. I have a javascript file where I make a REST call. The file looks like this:
var data;
function getDataRequest() {
const request = new XMLHttpRequest();
request.open('GET', "localhost:8080/getData", true);
request.onload = function () {
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
products = data;
} else {
console.log('error');
}
};
request.send();
}
var getDataList = function() {
getDataRequest();
return data;
};
getDataList();
And the file which contains the React component looks like this:
var = data;
class Datas extends React.Component {
render() {
data = this.getDataList;
return (
<DataTable data={data} />
);
}
}
export default Products;
The problem is that data in the React component remains 'undefined'. I am making the rest call in a different file because I want to call the rest call from multiple react components.