Calling action from constructor vs a life cycle me

2019-02-09 02:04发布

问题:

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?

回答1:

It will be better to make the api calls from lifecycle method componentDidMount, react doc also suggested the same.

As per DOC:

componentDidMount:

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.



标签: reactjs redux