我需要使用从路由器/URL 的查询参数中获取的值来设置 React 组件的状态。
我在哪个生命周期方法中使用这些参数设置状态
constructor
, componentDidMount
, componentWillMount
或任何其他?
最佳答案
从网络请求加载数据的理想生命周期方法是 componentDidMount
https://reactjs.org/docs/react-component.html#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.
关于reactjs - 在哪里使用 URL 参数中的值设置 React 组件的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50420615/