React路由器导航栏示例

Kar*_*ikJ 47 nav reactjs react-router

我是React JS的初学者,我想为我的仪表板开发基于路由器的反应导航.该模型如下:

小样

我为尝试路由而创建的app.js代码如下:

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'
import Login from './components/Login.js';

const App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/login">Login</Link></li>
          <li><Link to="/inbox">Inbox</Link></li>
        </ul>
        {this.props.children}
      </div>
    )
  }
})


render((
  <li>
  <Router>
    <Route path="/" component={App}>
      <Route path="login" component={Login} />
    </Route>
  </Router>
  </li>
), document.getElementById('placeholder'))
Run Code Online (Sandbox Code Playgroud)

如何创建模型中显示的导航?

小智 40

是的,Daniel是正确的,但是为了扩展他的答案,您的主要应用程序组件需要在其中包含导航栏组件.这样,当您渲染主应用程序('/'路径下的任何页面)时,它也会显示导航栏.我猜您不希望您的登录页面显示导航栏,因此不应该是嵌套组件,而应该是它自己.所以你的路线最终会看起来像这样:

<Router>
  <Route path="/" component={App}>
    <Route path="page1" component={Page1} />
    <Route path="page2" component={Page2} />
  </Route>
  <Route path="/login" component={Login} />
</Router>
Run Code Online (Sandbox Code Playgroud)

其他组件看起来像这样:

var NavBar = React.createClass({
  render() {
    return (
      <div>
        <ul>
          <a onClick={() => history.push('page1') }>Page 1</a>
          <a onClick={() => history.push('page2') }>Page 2</a>
        </ul>
      </div>
    )
  }
});

var App = React.createClass({
  render() {
    return (
      <div>
        <NavBar />
        <div>Other Content</div>
        {this.props.children}
      </div>
    )
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 为什么使用history.push而不是<Link>? (23认同)

Chr*_*ris 39

注意接受完全没问题 - 但是想添加一个版本4示例,因为它们足够不同.

Nav.js

  import React from 'react';
  import { Link } from 'react-router';

  export default class Nav extends React.Component {
    render() {    
      return (
        <nav className="Nav">
          <div className="Nav__container">
            <Link to="/" className="Nav__brand">
              <img src="logo.svg" className="Nav__logo" />
            </Link>

            <div className="Nav__right">
              <ul className="Nav__item-wrapper">
                <li className="Nav__item">
                  <Link className="Nav__link" to="/path1">Link 1</Link>
                </li>
                <li className="Nav__item">
                  <Link className="Nav__link" to="/path2">Link 2</Link>
                </li>
                <li className="Nav__item">
                  <Link className="Nav__link" to="/path3">Link 3</Link>
                </li>
              </ul>
            </div>
          </div>
        </nav>
      );
    }
  }
Run Code Online (Sandbox Code Playgroud)

App.js

  import React from 'react';
  import { Link, Switch, Route } from 'react-router';
  import Nav from './nav';
  import Page1 from './page1';
  import Page2 from './page2';
  import Page3 from './page3';

  export default class App extends React.Component {
    render() {    
      return (
        <div className="App">
          <Router>
            <div>
              <Nav />
              <Switch>
                <Route exactly component={Landing} pattern="/" />
                <Route exactly component={Page1} pattern="/path1" />
                <Route exactly component={Page2} pattern="/path2" />
                <Route exactly component={Page3} pattern="/path3" />
                <Route component={Page404} />
              </Switch>
            </div>
          </Router>
        </div>
      );
    }
  }
Run Code Online (Sandbox Code Playgroud)

或者,如果您想要更加动态的导航,您可以查看优秀的v4文档:https://reacttraining.com/react-router/web/example/sidebar

  • 如果您有登录页面,这如何工作?可能不希望导航栏出现在登录页面上.有没有解决的办法? (6认同)
  • 当前的v4路由器只允许一个孩子.我通过使用单个<div />包装Router Nav/Switch子项来修复此问题.然后我必须在Switch之前将Nav放入<Route />,这样它就会收到路径道具历史,匹配和位置.请更新你的答案. (3认同)

Lui*_*v99 21

React 路由器版本 6+ 2021 带/不带导航栏的路线

import { Outlet, Route, Routes } from "react-router-dom";
// your components here 

const App = () => {
  
  return (
    <>
      <Routes>
        {/* Routes that needs a navbar will need to go as children of this Route component */}
        <Route path="/" element={<LayoutsWithNavbar />}>
          <Route path="/" element={<div>Home screen</div>} />
          <Route path="/welcome" element={<Welcome />} />
          <Route path="/something" element={<Somethinggg />} />
          <Route path="/somethingProtected" element={<YourCustomProtectedRoute component={Someone}/>} />
          <Route path="/something/:id" element={<ProtectedRoute id component={SomethingById}/>} />
        </Route>

        {/* Routes without a navbar you can add them here as normal routes */}
        <Route
          path="/idontneednavbar"
          element={<ProtectedRoute component={ProviderRegistrationInfo} />}
        />
      </Routes>
    </>
  );

  function LayoutsWithNavbar() {
    return (
      <>
        {/* Your navbar component */}
        <Navbar />
  
        {/* This Outlet is the place in which react-router will render your components that you need with the navbar */}
        <Outlet /> 
        
        {/* You can add a footer to get fancy in here :) */}
      </>
    );
  }
  
Run Code Online (Sandbox Code Playgroud)

怎么运行的

LayoutsWithNavbar组件将使用您需要的任何组件来渲染导航栏。您可以在此处的官方react-router v6文档中找到示例: https: //reactrouter.com/docs/en/v6/getting-started/overview#nested-routes

了解有关该Outlet组件的更多信息:https://reactrouter.com/docs/en/v6/getting-started/concepts#outlets