The componentWillMount()
life cycle event is actually the correct place to call fetchData()
because it is invoked once before the component mounts, this way you can setState do that the data is there when it mounts.
void componentWillMount()
Invoked once, both on the client and server, immediately before the initial rendering occurs. If you call setState within this method,render() will see the updated state and will be executed only once despite the state change.
Whereas componentDidMount()
renders after the component had already mounted.
void componentDidMount()
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The componentDidMount() method of child components is invoked before that of parent components.