简体   繁体   English

如何在反应路由器 v6 中为 / 和 home 创建路径?

[英]how to make path for / and home in react router v6?

this is my routes now, which don't work:这是我现在的路线,它不起作用:

const routes = [
  {
    path: "/",
    element: <Navbar />,
    children: [{ path: "/(home)?", element: <Home /> }],
  },
];

i remember working on path before for home like this:我记得以前在回家的路上工作是这样的:

<Route path="/(home)?" element={<Home />} />

i want to match / and /home and render Home component for both?我想匹配 / 和 /home 并为两者渲染 Home 组件?

You can pass an array of children routes for / and /home .您可以为//home传递一组子路由。

const routes = [
  {
    path: '/',
    element: <Navbar />,
    children: [
      { path: '/', element: <Home /> },
      { path: 'home', element: <Home /> },
    ],
  },
]

Note that the Navbar component should render Outlet to render the child routes.请注意, Navbar组件应呈现Outlet以呈现子路由。

import { Outlet } from 'react-router-dom'

const Navbar = () => (
  <>
    <h1>Navbar</h1>
    <Outlet />
  </>
)

编辑 reverent-ellis-cn8j5

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

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