[英]Rendering components inside Route React
I want to make to render children in father component, that's how i tried to make it but browser shows nothing eventually:我想在父亲组件中渲染孩子,这就是我试图做到的方式,但浏览器最终什么也没显示:
const MainLayout = props => {
return (
<div className='main-layout'>
<Header />
<div className='main'>
{props.children}
</div>
</div>
)
}
App.js应用程序.js
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route exact path="/" render={() => (
<MainLayout>
<Homepage />
</MainLayout>
)} />
</Routes>
</BrowserRouter>
You should use element
proprety to render your component, assuming that you are using React Router Dom V6, component
propriety for anterior versions, or simply like this, by wrapping inside the Route
:您应该使用
element
属性来呈现您的组件,假设您使用的是 React Router Dom V6,之前版本的component
属性,或者简单地像这样,通过包装在Route
中:
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route exact path="/">
<MainLayout>
<Homepage />
</MainLayout>
</Route>
</Routes>
</BrowserRouter>
</div>
)
Hope it helps...希望能帮助到你...
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route exact path="/" element={<MainLayout />}>
<Route path="/home" element={<Homepage />} />
</Route>
</Routes>
</BrowserRouter>
</div>
)
}
and Add a Outlet
component inside MainLayout Component并在 MainLayout 组件中添加一个
Outlet
组件
const MainLayout = props => {
return (
<>
<div className="main-layout">
<Header />
<div className="main">{props.children}</div>
</div>
<Outlet />
</>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.