setState() in componentWillMount()

Avoid async initialization in componentWillMount()

componentWillMount() is invoked immediately before mounting occurs.
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.

Make async calls for component initialization in componentDidMount instead of componentWillMount

  1. function componentDidMount() {
  2. axios.get(`api/messages`)
  3. .then((result) => {
  4. const messages = result.data
  5. console.log("COMPONENT WILL Mount messages : ", messages);
  6. this.setState({
  7. messages: [...messages.content]
  8. })
  9. })
  10. }