It is recommended to avoid asynchronous initialisation in the
componentWillMount()
lifecycle method. componentWillMount()
is invoked immediately before mounting. It is called before render()
, therefore setting state in this method will not trigger a re-render. Avoid introducing any side effects or subscriptions in this method. We need to make sure that componentDidMount()
instead of asynchronous calls to initialise the component happened during initialisation componentWillMount()
.
componentDidMount() {
axios.get(`api/todos`)
.then((result) => {
this.setState({
messages: [...result.data]
})
})
}
Comments