For what I understand now, if we pass an Outlet
with context, the props after the context could be passed into child, and outlet act as a template that pass those props into any child the router may render.
My question is, what if we just set <Outlet />
without context? If it doesn't pass any props, is there any reason why we particularly want to use <Outlet />
alone?
Code may look like this
index.js
root.render(
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route path="checkout" element={<Checkout />} />
</Route>
</Routes>
</BrowserRouter>
);
App.js
function App() {
return (
<>
<Reset />
<GlobalStyle />
<Header cartItems={cartItems} />
<Outlet />
</>
)
}
CodePudding user response:
The Outlet
component alone allows nested routes to render their element
content out and anything else the layout route is rendering, i.e. navbars, sidebars, specific layout components, etc.
<Routes>
<Route path="/" element={<App />}>
<Route path="checkout" element={<Checkout />} />
</Route>
</Routes>
The Checkout
component is rendered on "/checkout"
. The Reset
, GlobalStyle
, and Header
components are all also rendered as part of the layout route "/"
rendering App
. I.E. something like the following is rendered to the DOM.
...
<Reset />
<Header cartItems={cartItems} />
<Checkout />
...