简体   繁体   English

反应路由器提供子路径以再次单击链接--v6

[英]react router giving sub path for clicking again on link --v6

here I am building a website and I am using react-router-dom everything seems to work fine when I use the Navbar and Footer components in every page.在这里,我正在构建一个网站,并且正在使用react-router-dom ,当我在每个页面中使用NavbarFooter组件时,一切似乎都运行良好。 So I come up with idea to wrap the component in a wrapper which contains Navbar and Footer .所以我想出了将组件包装在包含NavbarFooter的包装器中的想法。

When I click on Link or maybe NavLink , it seems to work fine but when I click again on same or another link in navbar then it navigates to a sub path under the previous selected path.当我单击LinkNavLink时,它似乎工作正常,但是当我再次单击导航栏中的相同或另一个链接时,它会导航到上一个选定路径下的子路径。 like this像这样
on Single Click:单击一次:

http://localhost:3000/projects

on clicking the same link again:再次单击相同的链接:

http://localhost:3000/projects/projects

App.js应用程序.js

import './App.css';
import {BrowserRouter as Router, Route, Outlet, Routes} from 'react-router-dom'
import {CommonScreen, Wrapper, About, Email, Projects} from './Barell'

function App() {
  return (
    <>
    <Router>
      <Routes>
        <Route  index  element={<Wrapper children={<CommonScreen/>}/>}/>
        <Route  path='about'  element={<Wrapper children={<About/>}/>}/>
        <Route  path='projects'  element={<Wrapper children={<Projects/>}/>}/>
        <Route  path='email'  element={<Email/>}/>
      </Routes>
    </Router>
    <Outlet/>
    </>
  );
}

export default App;

Navbar.jsx:导航栏.jsx:

import React from 'react'
import '../../index.css'
import { NavLink } from 'react-router-dom'

const links = [
  {
    name:"Home",
    slug:"/",
  },
  {
    name:"Projects",
    slug:"projects",
  },
  {
    name:"About",
    slug:"about",
  },
  {
    name:"Contact",
    slug:"email",
  },
]
export const Navbar = () => {

  let activeStyle = {
    textDecoration: "underline",
  };


  return (
    <>
      <nav>
        <div id='brand'>Prema<span>culture</span></div>
        <ul id='links'>
         {
          links.map((current,index) => (
            <li>
              <NavLink 
              key={index}
              to={current.slug}
              style={({ isActive }) =>
              isActive ? activeStyle : undefined
            }
              >
                {current.name}
              </NavLink>
            </li>
          ))
         }
        </ul>
      </nav>
    </>
  )
}

Wrapper.jsx:包装器.jsx:

import React from 'react'
import { Navbar, Footer } from '../Barell'

export const Wrapper = ({children}) => {
  return (
    <>
    <Navbar/>
    {children}
    <Footer/>
    </>
  )
}

You are using relative paths ( versus absolute ), so when already on a route, say "/projects" , and then click a link that navigates to "about" , the code links relative from "/projects" and results in "/projects/about" .您正在使用相对路径(相对于绝对路径),因此当已经在路线上时,说"/projects" ,然后单击导航到"about"的链接,代码链接相对于"/projects"并导致"/projects/about"

To resolve you can make all the link paths absolute by prefixing a leading "/" character.要解决此问题,您可以通过为前导"/"字符添加前缀来使所有链接路径成为绝对路径。 Absolute paths begin with "/" .绝对路径以"/"开头。

Example:例子:

const links = [
  {
    name:"Home",
    slug:"/",
  },
  {
    name:"Projects",
    slug:"/projects",
  },
  {
    name:"About",
    slug:"/about",
  },
  {
    name:"Contact",
    slug:"/email",
  },
];

Additionally, to make the code more DRY you might also want to convert the Wrapper component into a layout route .此外,为了使代码更加DRY ,您可能还希望将Wrapper组件转换为布局路由 Layout routes render an Outlet component for nested routes to be rendered into instead of a children prop for a single routes content.布局路由为嵌套路由渲染一个Outlet组件,而不是为单个路由内容渲染一个children prop。

Example例子

import React from 'react';
import { Outlet } from 'react-router-dom';
import { Navbar, Footer } from '../Barell';

export const Layout = () => {
  return (
    <>
      <Navbar/>
      <Outlet />
      <Footer/>
    </>
  )
}

... ...

function App() {
  return (
    <>
      <Router>
        <Routes>
          <Route element={<Layout />}>
            <Route path="/" element={<CommonScreen />} />
            <Route path='about' element={<About />} />
            <Route path='projects' element={<Projects />} />
          </Route>
          <Route path='email' element={<Email />} />
        </Routes>
      </Router>
      <Outlet />
    </>
  );
}

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

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