简体   繁体   English

react-router-dom 不适用于我的项目

[英]react-router-dom doesn't work for my project

I installed react-router-dom to switch between navbar elements.我安装了 react-router-dom 来在导航栏元素之间切换。 The library does not want to cooperate with my project.图书馆不想与我的项目合作。 After clicking on the navbar element I am not redirected to the required component.单击导航栏元素后,我没有被重定向到所需的组件。 Sometimes when I click on a selected item the menu moves slightly to the left.有时,当我单击所选项目时,菜单会稍微向左移动。 My code looks like this:我的代码如下所示:

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';


ReactDOM.render(
  <App />, document.getElementById('root'));

Navbar.js导航栏.js

import React, { useState } from "react";
import App from '../components/App'
import About from '../components/About';
import Services from '../components/Services';
import Contact from '../components/Contact';
import {
    BrowserRouter as Router,
    Switch,
    Route,
    Link
  } from "react-router-dom";



  

const Navbar = () => {

        const [navLinkOpen, navLinkToggle] = useState(false);

        const handleNavLinksToggle = () => {
            navLinkToggle(!navLinkOpen);
        };

        const renderClasses = () => {
            let classes = "navlinks";
            

            if(navLinkOpen) {
                classes += " ' ' + active";
            }
            return classes;
        };
    return (
        <nav>
            <div className="logo">
                <h4>Delightartco</h4>
            </div>
                <ul className={renderClasses()}>
                    <Router>
                        <li className="link"><Link to={"/home"}>Home</Link></li>
                        <li className="link"><Link to={"/about"}>About</Link></li>
                        <li className="link"><Link to={"/services"}>Services</Link></li>
                        <li className="link"><Link to={"/contact"}>Contact</Link></li> 
                    <Switch>
                        <Route path="/home" component={App}>
                        </Route>
                        <Route path="/about" component={About}>
                        </Route>
                        <Route path="/services" component={Services}>
                        </Route>
                        <Route path="/contact" component={Contact}>
                        </Route>
                    </Switch>
                    </Router>     
                </ul>
            <div onClick={handleNavLinksToggle} className="hamburger-toggle">
                <i className="fas fa-bars fa-lg"></i>
            </div>
        </nav>
    )
}

export default Navbar;

App.js应用程序.js

import React from 'react';
import '../../src/App.css';
import Navbar from './Navbar';
import Wrapper from './Wrapper';
import {Content, Winnie, Testimonials, Values, Secrets, Footer} from '../components/Content';



function App() {
      return (
          <div>
            <Navbar />
            <Wrapper />
            <Content />
            <Winnie />
            <Testimonials />
            <Values />
            <Secrets />
            <Footer />
          </div>
      )
}


export default App;

There are several things to keep in mind when using react-router .使用react-router时要记住几件事。

  1. The Router or BrowserRouter component should wrap all your routes and your links. RouterBrowserRouter组件应该包装您的所有路由和链接。 Generally, if your app does not need more than one Router , its better to wrap your whole App with the Router.通常,如果您的应用程序不需要多个Router ,最好用路由器包装整个应用程序。

  2. The Link component's job is to simply navigate to the page and can be used anywhere you want to show a link to someplace eg in the Navbar . Link组件的工作是简单地导航到页面,并且可以在任何你想显示到某个地方的链接的地方使用,例如在Navbar中。

  3. The Route (not Router) component's placement is very important. Route (不是Router)组件的放置非常重要。 It should be placed where you want to render the content.它应该放置在您要呈现内容的位置。 In your code you are rendering the routes in the Navbar and are unable to see the routes being rendered due to invalid / improper structure.在您的代码中,您正在Navbar呈现路线,并且由于无效/不正确的结构而无法看到正在呈现的路线。

Navbar.js导航栏.js

Your Navbar should only contain the links while the Router should be on the top-level and the Switch / Routes should be placed where you want to render the content.您的导航栏应仅包含链接,而Router应位于顶层,而Switch / Routes应放置在您要呈现内容的位置。

function Navbar() {
  return (
    <nav>
      {/* Move `Router` to top-level e.g. in App.js */}
      <ul>
        <li className="link">
          <Link to={"/home"}>Home</Link>
        </li>
        <li className="link">
          <Link to={"/about"}>About</Link>
        </li>
        <li className="link">
          <Link to={"/services"}>Services</Link>
        </li>
        <li className="link">
          <Link to={"/contact"}>Contact</Link>
        </li>
      </ul>
      {/* Move `Switch and Routes` to where you want to render the content e.g. in Content.js */}
    </nav>
  );
}

App.js应用程序.js

function App() {
  return (
    <Router>
      <div>
        <Navbar />
        <Wrapper />
        <Switch>
          <Route path="/home" component={App}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/services" component={Services}></Route>
          <Route path="/contact" component={Contact}></Route>
        </Switch>
        <Winnie />
        <Testimonials />
        <Values />
        <Secrets />
        <Footer />
      </div>
    </Router>
  );
}

These are few issues in your code:这些是您的代码中的几个问题:

  1. App is your root React component, and you gave it a route: <Route path="/home" component={App}></Route> . App是你的根 React 组件,你给了它一个路由: <Route path="/home" component={App}></Route> This is causing a recursive / infinite loop.这导致递归/无限循环。 App component inside App component. App组件内的App组件。
  2. Code structure looks complex.代码结构看起来很复杂。

Here is a proposed fixed code:这是一个建议的固定代码:

index.jsx: index.jsx:

ReactDOM.render(<App />, document.getElementById("root"));

App.jsx:应用程序.jsx:

export default function App() {
  return (
    <StrictMode>
      <Routes />
    </StrictMode>
  );
}

Routes.jsx:路线.jsx:

export default function Routes() {
  return (
    <Router>

      {/* Route components would be visible only at their route */}
      <Switch>
        <Route exact path="/about" component={About}></Route>
        <Route exact path="/services" component={Services}></Route>
        <Route exact path="/contact" component={Contact}></Route>
      </Switch>

      {/* Below components would be visible always at UI */}
      <Navbar />     {/* Top navigation Link's */}
      <Wrapper />
      <Content />
      <Winnie />
      <Testimonials />
      <Values />
      <Secrets />
      <Footer />     {/* Bottom navigation Link's */}
    </Router>
  );
}

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

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