React docs says that I should make AJAX calls to set the state in componentDidMount (), and not in the constructor. According to all the examples I saw, I have to download data from the server:
constructor() {
super();
this.state = {toDoItems: []};
}
componentDidMount() {
loadToDoItems()
.then(items => this.setState({toDoItems: items}));
}
Why not just load the server data in the constructor, for example:
constructor() {
super();
loadToDoItems()
.then(items => this.state = {toDoItems: items});
}
source
share