reactjs - 这个 componentWillMount 案例的替代方案?
问题描述
我正在尝试重构一些旧代码。
componentWillMount
已被弃用。
我如何将它在这里重构为其他东西?
class Detail extends React.Component{
constructor(props) {
super(props);
this.state = {
logged_out_redirect: true
};
}
componentWillMount() { //<------ this lifecycle method has been deprecated, how do I replace it?
if (localStorage.getItem("JWT")) {
this.setState({logged_out_redirect: false});
}
}
render() {
if (this.state.logged_out_redirect) {
return (<Redirect to={"/login"}/>)
}
return ( )
}
}
解决方案
在这种情况下(取决于你想要做什么componentWillMount
)。你不能把它放在你的constructor()
constructor(props) {
super(props);
this.state = {
logged_out_redirect: localStorage.getItem("JWT") ? false : true
};
}
推荐阅读
- python - 假日季节性在 FBProphet 中全为零
- python - 复制目录而不是文件时,shutil.copy() 拒绝权限
- r - 访问动态创建的 Shiny 模块的返回值
- python - Python:点的顺序在 Hessian 曲线实现中不规则
- snakemake - Snakemake:重用中间输出文件的参数化运行
- web-scraping - 如何访问对象标签的内部内容?
- ios - 在另一个创建 rxswift 中订阅单个 observable
- azure - WARN 无法为 plottable 找到 package.json
- javascript - 通过不在 Firebase 实时数据库中工作的无限滚动顺序
- r - R中的k-fold嵌套重复交叉验证