我遇到了一些与反应路由器导航相关的问题。在我的导航中有两个菜单登录和注册。我的索引页面是登录。
在登录页面上输入电子邮件和密码的值并提交登录按钮后,它会输出为 sessionValue。我的 sessionValue 是从服务器端返回的用户名,它重定向到 TodoApp 页面。登录后,它重定向到 TodoApp 页面。我想显示注销菜单而不是在导航中登录。
Myy代码如下:
app.jsx
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={Main}>
<Route path="RegistrationForm" component={RegistrationForm}/>
<Route path="todoapp/:sessionValue"component={TodoApp}/>
<IndexRoute component={Login}/>
</Route>
</Router>,
document.getElementById('app')
);
导航组件
我尝试使用本地存储,但它不起作用。仅当我单击浏览器的后向箭头时,它才有效。
var Nav= React.createClass({
render:function(){
var strUserName = localStorage.getItem('sessionValue');
console.log(strUserName);
function loginMenu(){
if(strUserName){
return <Link to="/RegistrationForm" activeClassName="active" activeStyle={{fontWeight: 'bold'}}>Logout</Link>
}
else{
return <IndexLink to="/" activeClassName="active" activeStyle={{fontWeight: 'bold'}}>LogIn</IndexLink>
}
}
return(
<div className="top-bar">
<div className="top-bar-left">
<ul className="menu">
<li className="menu-text">
React App
</li>
<li>
{loginMenu()}
</li>
<li>
<Link to="/RegistrationForm" activeClassName="active" activeStyle={{fontWeight: 'bold'}}>Signup</Link>
</li>
</ul>
</div>
</div>
);
}
});
module.exports=Nav;
登录页面(login.jsx)
if(validForm){
axios.post('/login', {
email:this.state.strEmail,
password:this.state.strPassword
})
.then(function (response) {
//console.log(response.data);
var sessionValue=response.data;
//After login todoApp page is open and with session value.session value passed through url to todoApp.
browserHistory.push(`todoapp/${sessionValue}`);
localStorage.setItem('sessionValue',sessionValue);
})
.catch(function (error) {
console.log(error);
});
}
},
问题:
如何显示/隐藏登录/注销菜单。
登录后显示注销菜单,当我们单击注销菜单时显示登录菜单