【发布时间】:2019-10-24 07:06:47
【问题描述】:
所以我有一个特定的代码。 问题是它不能在某些时候起作用——在它应该起作用的时候。 我需要刷新页面才能工作。
如果来自 LocalStorage 的用户为 null,则应呈现 如果来自 LocalStorage 的用户存在,则不应呈现
它的工作原理,但我怎样才能改进它登录后工作(而不是刷新页面后)?
我应该使用其他反应生命周期方法吗?
class Navbar extends React.Component {
constructor(props){
super(props)
this.state ={
user: null
}
}
componentDidMount(){
const user = JSON.parse(localStorage.getItem('user'));
this.setState({
user: user
},
console.log('test'+ this.state.user)
)
}
render(){
return(
<div>
{ !this.state.user ?
<div className ="navbar">
<Logo/>
<Menu/>
<PanelOptions/>
</div>
: null}
</div>
);
}
}
【问题讨论】:
标签: reactjs components rendering roles