I am trying to fetch some data from API and loop through the parsed response content and render them.
But I am getting a warning
webpackHotDevClient.js:138 ./src/App.js
Line 20: Expected an assignment or function call and instead saw an expression no-unused-expressions
Here's my App.js:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import './Api.css';
class App extends Component {
render() {
var request = new XMLHttpRequest();
var listItems;
request.open('GET', 'https://ghibliapi.herokuapp.com/films', true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400)
{
listItems = data.forEach(movie => {
<div className="card">
<h1>{movie.title}</h1>
<p>{movie.description}</p>
</div>
});
}
else {
console.log('error');
}
}
request.send();
return (
<div className="App">
<img src={logo} className="App-logo" alt="logo" />
<div className="container">
{listItems}
</div>
</div>
);
}
}
export default App;
When I try to console.log(listItems) after request.send() I get undefined.
I assume undefined issue could be because of asynchronous behavior.
How can I render these elements?