I am fetching data from my API and need to construct a list based on the received data. The problem is, the rendering happens earlier than the fetching in the constructor is completed so the this.todo
sent to List
component happens to be empty. What is the correct way to handle this?
import React, { Component } from 'react';
import {render} from 'react-dom';
import Header from './header.jsx'
import List from './list.jsx'
class App extends Component {
constructor(props) {
super(props)
this.todos = []
const url = "http://localhost:3000/api/todos/"
fetch(url)
.then(res => res.json())
.then((data) => {
this.todos = data;
})
.catch(console.error);
}
render () {
return (
<div>
<Header />
<List tasks={this.todos}/>
</div>
)
}
}
render(<App/>, document.getElementById('app'));