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.