componentDidMount and constructor componentDidMount and constructor reactjs reactjs

componentDidMount and constructor


The Problem is that when you place an async request in constructor, the promise might be resolved after render phase has executed and at that point this.state is null and also since you just assign

this.state = {    array: arr}

and this won't lead to a re-render and so the component won't reflect the change. Having said that you should place your async requests in componentDidMount which you mention in your second attempt, and since you call setState there, a re-render is triggered and the state in reflected in render