ReactJs React-Router Navigation

时间:2017-05-16 02:10:12

标签: reactjs navigation react-router

我遇到了与react-router导航相关的一些问题。在我的导航中有2个菜单登录和注册,我的索引页面是登录。

在登录页面输入emaild和密码的值并提交登录按钮后,它将输出为sessionValue.My sessionValue是从serverSide返回的用户名,并重定向到TodoApp页面。登录后,它会重定向到TodoApp页面。我想显示注销菜单而不是登录导航。

我的代码如下:

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')
);

导航组件

我通过使用localstorage尝试了这个,但它不起作用。它仅在我点击浏览器的向后箭头时才有效。

 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);
        });
    }
}, 

问题:

  1. 如何显示/隐藏登录/退出菜单。
  2. 登录后显示注销菜单,点击退出菜单显示登录菜单

1 个答案:

答案 0 :(得分:2)

您应该在场景后面有一些状态管理策略来实现此行为。因此,您可以拥有以下组件结构

App
- Nav
- LoginForm

如果没有像redux / mobX这样的状态管理库,则需要在App组件中定义状态

class App extends Component {
  constructor(props) {
    super(props);
    this.onLoggedInChanged = this.onLoggedInChanged.bind(this);
    this.state = {
      isLoggedIn: false
    }
  }
  onLoggedInChanged(isLoggedIn) {
    this.setState({
      isLoggedIn: isLoggedIn
    });
  }
  render() {
    return (
      <Nav isLoggedIn={this.state.isLoggedIn} />
      <LoginForm onLoggedInChanged={this.onLoggedInChanged}></LoginForm>
    );
  }
}

然后在导航组件中,您可以使用isLoggedIn标志

Nav = () => {
  let loginElement = this.props.isLoggedIn ? 'Logged in user' : 'Not logged in'
  return (<div>{loginElement }</div>)
}

您可以在https://facebook.github.io/react-native/docs/state.html

的文档中找到有关反应应用状态的更多信息

在redux / mobx中,它将以不同的方式完成。