嵌套路径在React Router DOM中不起作用。

huangapple go评论77阅读模式
英文:

nested path dosent work in react router dom

问题

在这段代码中,"localhost:3000/""localhost:3000/dashboard" 都只打开 HomeScreen 页面。您想要让 "/dashboard" 打开 LoginScreen 而不是 HomeScreen,您可以这样做:

确保您的路由配置正确,您可以使用 <Route path="/dashboard" element={<LoginScreen />} /> 来指定 /dashboard 路径应该渲染 LoginScreen 组件。

以下是更改后的代码片段:

import React from 'react';
import './App.css';
import HomeScreen from './screens/HomeScreen';
import {
  createBrowserRouter,
  createRoutesFromElements,
  Route,
  RouterProvider
} from 'react-router-dom';
import LoginScreen from './screens/LoginScreen';

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<HomeScreen />} />
    <Route path="/dashboard" element={<LoginScreen />} />
    {/* ... etc. */}
  )
);

function App() {
  return (
    <div className="App">
      <RouterProvider router={router} />
    </div>
  );
}

export default App;

这样,当访问 &quot;/dashboard&quot; 时,应该渲染 LoginScreen 页面而不是 HomeScreen 页面。

英文:

I am trying to create a browser router for my app but the nested paths are not working.

import React from &#39;react&#39;;
import &#39;./App.css&#39;;
import HomeScreen from &#39;./screens/HomeScreen&#39;;
import {
  createBrowserRouter,
  createRoutesFromElements,
  Route,
  RouterProvider
} from &#39;react-router-dom&#39;;
import LoginScreen from &#39;./screens/LoginScreen&#39;;

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&quot;/&quot; element={&lt;HomeScreen /&gt;}&gt;
      &lt;Route path=&quot;dashboard&quot; element={&lt;LoginScreen /&gt;} /&gt;
      {/* ... etc. */}
    &lt;/Route&gt;
  )
);

function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;RouterProvider router={router} /&gt;
    &lt;/div&gt;
  );
}

export default App;

On this code both &quot;localhost:3000/&quot; and &quot;localhost:3000/dashboard&quot; are opening the HomeScreen page only.
Can you tell me what I should do to make &quot;/dashboard&quot; open LoginSrceen instead of HomeScreen?

答案1

得分: 1

如果你嵌套了路由,那么父路由必须渲染一个 Outlet 组件,以便嵌套路由将其 element 内容渲染到这里。

示例:

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

const HomeScreen = () => {
  ...

  return (
    ... 主屏幕 UI ...
    <Outlet /> // <-- 嵌套路由在这里渲染内容
    ...
  );
};
const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<HomeScreen />}>
      <Route path="dashboard" element={<LoginScreen />} />
      {/* ... 等等 */}
    </Route>
  )
);

function App() {
  return (
    <div className="App">
      <RouterProvider router={router} />
    </div>
  );
}

假设你真的想让 HomeScreenLoginScreen 在各自独立的路由上渲染,那么它们不应该嵌套在彼此之内,而应该作为兄弟路由渲染。在索引路由上渲染 HomeScreen,这样当 URL 路径匹配父路由时,它将被渲染,例如 "/"

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/">
      <Route index element={<HomeScreen />} />
      <Route path="dashboard" element={<LoginScreen />} />
      {/* ... 等等 */}
    </Route>
  )
);

function App() {
  return (
    <div className="App">
      <RouterProvider router={router} />
    </div>
  );
}
英文:

If you are nesting routes then the parent route necessarily needs to render an Outlet component for nested routes to render their element content into.

Example:

import { Outlet } from &#39;react-router-dom&#39;;

const HomeScreen = () =&gt; {
  ...

  return (
    ... Home screen UI ...
    &lt;Outlet /&gt; // &lt;-- nested routes render content here
    ...
  );
};
const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&quot;/&quot; element={&lt;HomeScreen /&gt;}&gt;
      &lt;Route path=&quot;dashboard&quot; element={&lt;LoginScreen /&gt;} /&gt;
      {/* ... etc. */}
    &lt;/Route&gt;
  )
);

function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;RouterProvider router={router} /&gt;
    &lt;/div&gt;
  );
}

On the off-hand chance that you actually want HomeScreen and LoginScreen to render each on their own discrete routes, then they should not be nested one in the other, but instead should be rendered as sibling routes. Render HomeScreen on an index route such that it will be rendered when the URL path matches the parent route, e.g. &quot;/&quot;.

const router = createBrowserRouter(
  createRoutesFromElements(
    &lt;Route path=&quot;/&quot;&gt;
      &lt;Route index element={&lt;HomeScreen /&gt;} /&gt;
      &lt;Route path=&quot;dashboard&quot; element={&lt;LoginScreen /&gt;} /&gt;
      {/* ... etc. */}
    &lt;/Route&gt;
  )
);

function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;RouterProvider router={router} /&gt;
    &lt;/div&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月18日 23:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定