简体   繁体   English

React Router导航栏导航

[英]React router navbar navigation

Im new to react and im having issues with the routing. 我是新来的人,对路由有问题。 I want the navbar to render on some pages but not others.What is the correct way to do this? 我希望导航栏显示在某些页面上而不显示在其他页面上。执行此操作的正确方法是什么? I know im not supposed to render it in specific components. 我知道我不应该在特定组件中渲染它。

so if this is my app.js 所以如果这是我的app.js

var App = React.createClass ({
  render (){
    return (
      <div>
        <NavbarInstance />
      </div>
    );
  }
});


document.write('<div id="container"></div>');
ReactDom.render(
  <Router history={createHistory({ queryKey: false })}
        onUpdate={() => window.scrollTo(0, 0)}>
    <Route path="/" component={App} />
    <Route path="/login" component={LoginForm} />
    <Route path="/other" component={Other} />
  </Router>,
  document.getElementById('container')
);

and this is my login page(see navbar instance-incorrect) 这是我的登录页面(请参阅navbar instance-incorrect)

import React from 'react';
import Input from 'react-bootstrap/lib/Input';
import NavbarInstance from './components/header.jsx';

const LoginForm = React.createClass({

    getInitialState: function() {
      return {
        username:'',
        password:''
      }
    },

  render () {
    return (
      <div>
        <NavbarInstance />
        <div className = "col-sm-12">
          <h3> Log In </h3>
          </div>
          <div className ="col-sm-6">
          <Input type = "text"
            placeholder ="Username"
            />
            <Input type= "password"
              placeholder="Password"
            />
          </div>
      </div>
    )
  }
})

export default LoginForm

One option would be to keep track of what page you are in within a state variable within your main App component. 一种选择是在主App组件的状态变量中跟踪您所处的页面。 Then add a check (say you didn't want to render it if page is 'index'. 然后添加检查(例如,如果页面为“索引”,则您不想呈现它。)

return (
  { if (this.state.page !== 'index')
      return <NavbarInstance />
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM