简体   繁体   English

嵌套路由在 React Router v6 中不起作用

[英]Nested routing is not working in React Router v6

I am trying react router v6 .我正在尝试反应路由器 v6 As per react training blog , I have created object of routing and passed to useRoutes() :根据反应培训博客,我创建了路由的 object 并传递给useRoutes()

function SomeOtherElement() {
  return <h1>add user</h1>;
}

const routes = [
  {
    path: 'app',
    element: <DashboardLayout />,
    children: [
      { path: 'account', element: <AccountView /> },
      {
        path: 'users', element: <UserListView />, 
        children: [
          { path: 'add', element: <SomeOtherElement /> }
        ]
      },
      { path: 'dashboard', element: <DashboardView /> },
      { path: 'products', element: <ProductListView /> },
      { path: 'settings', element: <SettingsView /> },
      { path: '*', element: <Navigate to="/404" /> }
    ]
  }];

const routing = useRoutes(routes);

But the nested routing is not working.但是嵌套路由不起作用。 As you can see in above object, I want to create URL and render the UI for user " add " functionality.正如您在上面的 object 中看到的,我想创建 URL 并为用户“添加”功能呈现 UI。

URL in the browser is getting updated correctly to http://localhost:3000/app/users/add but UI is not updating.浏览器中的 URL 已正确更新为http://localhost:3000/app/users/add但 UI 未更新。

As explained here , you need to use an <Outlet /> element as a placeholder for child ie nested routes.正如解释在这里,你需要使用<Outlet />元素作为子即嵌套路线的占位符。

Example:例子:

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

// ... 

const routes = [
  {
    path: "app",
    element: (
      <>
        <DashboardLayout />
        <Outlet />
      </>
    ),
    children: [
      { path: "account", element: <AccountView /> },
      {
        path: "users",
        element: (
          <>
            <UserListView />
            <Outlet />
          </>
        ),
        children: [{ path: "add", element: <SomeOtherElement /> }],
      },
      { path: "dashboard", element: <DashboardView /> },
    ],
  },
];

Or you may want to have Outlet inside your parent components:或者你可能希望在你的父组件中有Outlet

export default function DashboardLayout() {
  return (
    <>
      <h1>I am Dashboard Layout</h1>
      <Outlet />
    </>
  );
}
<BrowserRouter>
            <Routes>

                <Route path="/">
                    <Route index element={<Home/>}/>
                    <Route path="teams">
                        <Route index element={<ParentTeam/>}/>
                        <Route path=":teamId" element={<Team/>}/>
                        <Route path="new" element={<NewTeamForm/>}/>
                    </Route>
                </Route>
                
            </Routes>
        </BrowserRouter>

The default behavior of React Router fails to render multiple child routes with single Outlet. React Router 的默认行为无法使用单个 Outlet 渲染多个子路由。 For example - When there is requirement to have a single Header in all the pages and replace the common content (Single Outlet) based on Route.例如 - 当需要在所有页面中具有单个 Header 并根据 Route 替换公共内容(Single Outlet)时。

The trick here is to not provide any element attribute for parent but to provide this component as a child route with the help of Index attribute.这里的技巧是不为父元素提供任何元素属性,而是在索引属性的帮助下将此组件作为子路由提供。

Index helps us to have a default component for a parent route if the child routes doesn't match any of the child routes.如果子路由不匹配任何子路由,索引可以帮助我们为父路由设置默认组件。

The code snippet for this would be similar to the one provided by Samira.此代码片段将类似于 Samira 提供的代码片段。

<BrowserRouter>
  <Routes>
    <Route path="/" element={<App />}>
      <Route path="" element={<Home />}></Route>
      <Route path="child1"> //no element attribute here
        <Route index={true} element={<Child1 />}></Route>
        <Route path="grandChild1" element={<GrandChild1 />}></Route>
        <Route path="grandChild2" element={<GrandChild2 />}></Route>
      </Route>
    </Route>
  </Routes>
</BrowserRouter>

For the useRoutes hook the syntax looks like this:对于useRoutes挂钩,语法如下所示:

const items = [
  {
    path: "/login",
    element: <Login />,
  },
  {
    path: "/logout",
    element: <Logout />,
  },
  {
    path: "/",
    element:  <RequireAuth element={<Welcome />} />,
  },
  {
    path: "/inbox",
    element:  <RequireAuth element={<Inbox />} />
  },
  {
    path: "/dashboard",
    children: [
      {
        index: true,
        element:  <RequireAuth element={<Dashboard />} />
      },
      {
        path: "setup",
        element:  <RequireAuth element={<DashboardSetup/>} />
      },
    ]
  },


]

export default function MainContent() {
  const router = useRoutes(items);
  return router

}

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

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