gpt4 book ai didi

reactjs - 从构造函数调用操作与生命周期方法

转载 作者:行者123 更新时间:2023-12-03 13:08:40 25 4
gpt4 key购买 nike

哪里是调用 API 调用以补充我的状态的操作的最佳位置?构造函数或生命周期方法之一,例如组件将安装吗?

最佳答案

最好从生命周期方法 componentDidMount 进行 api 调用,react doc 也建议了同样的做法。

根据 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 - 从构造函数调用操作与生命周期方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com