英文:
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;
这样,当访问 "/dashboard"
时,应该渲染 LoginScreen
页面而不是 HomeScreen
页面。
英文:
I am trying to create a browser router for my app but the nested paths are not working.
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. */}
</Route>
)
);
function App() {
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
export default App;
On this code both "localhost:3000/"
and "localhost:3000/dashboard"
are opening the HomeScreen
page only.
Can you tell me what I should do to make "/dashboard"
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>
);
}
假设你真的想让 HomeScreen
和 LoginScreen
在各自独立的路由上渲染,那么它们不应该嵌套在彼此之内,而应该作为兄弟路由渲染。在索引路由上渲染 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 'react-router-dom';
const HomeScreen = () => {
...
return (
... Home screen UI ...
<Outlet /> // <-- nested routes render content here
...
);
};
const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<HomeScreen />}>
<Route path="dashboard" element={<LoginScreen />} />
{/* ... etc. */}
</Route>
)
);
function App() {
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
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. "/"
.
const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/">
<Route index element={<HomeScreen />} />
<Route path="dashboard" element={<LoginScreen />} />
{/* ... etc. */}
</Route>
)
);
function App() {
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论