reactjs - 在哪里使用 URL 参数中的值设置 React 组件的状态

标签 reactjs react-router lifecycle

我需要使用从路由器/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/

相关文章:

javascript - "this.state"或 Reactjs 中无法访问方法

Angular 生命周期钩子(Hook)

reactjs - React Router ,使用查询字符串分页

reactjs - 错误: Could not find router reducer in state tree,它必须安装在 "router"下

javascript - Nextjs 删除工作箱 console.log 消息

javascript - 手动刷新或写入时,React-router url 不起作用

android - 有什么方法可以在 Android 模拟器中强制应用程序内存不足?

lifecycle - Android应用程序类方法onCreate被多次调用

reactjs - Material UI 切换按钮 - 选择时无法更改背景颜色

javascript - 通过在 typescript 中渲染的字体颜色使用react