提供以下沙箱,不确定要了解如何呈现Bb组件
import React from "react";
import "./styles.css";
import { Routes, Route } from "react-router";
import { BrowserRouter } from "react-router-dom";
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path="a" element={<Aa />}>
<Route path="b" element={<Bb />} />
</Route>
</Routes>
</BrowserRouter>
);
}
function Aa() {
return (
<p>
Hello
</p>
);
}
function Bb() {
return <p>World</p>;
}
https://codesandbox.io/s/still-shadow-337zc?file=/src/App.js
浏览到/ a / b仅呈现Aa组件
react-router v6 exposes an
Outlet
element that you must use inside your parent Route so that any nested children can be rendered by react-router-dom isnide of itWorking demo
If you want only
Bb
to render ata/b
you define it as a separate route like@Shubham Khatri的答案的其他说明:
/a/b/
will renderIf you want to display only
Bb
component, nested route is not the right way.