Reputation: 30342
Where's the best place to call an action that will make an API call to hydrate my state? Constructor or one of the life cycle methods e.g. ComponentWillMount?
Upvotes: 21
Views: 10632
Reputation: 104459
It will be better to make the api calls from lifecycle method componentDidMount
, react doc also suggested the same.
As per DOC:
componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.
Upvotes: 16