Calling action from constructor vs a life cycle method

前端 未结 1 1003
甜味超标
甜味超标 2020-12-16 11:24

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条回答
  • 2020-12-16 12:27

    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.

    0 讨论(0)
提交回复
热议问题