Why my outlet is not working in react-router-dom
. All my components worked fine until I use Outlet
and after using outlet my navigation component is not showing while other component seems to render.
import Home from "./Routes/Home/Home.component";
import { Routes, Route, Outlet } from "react-router-dom";
const Navigation = () => {
return (
<div>
<div>
<h1>Hello I am Navigation!!!</h1>
</div>
<Outlet />
</div>
);
};
const Shop = () => {
return <h2>I am shop component</h2>
}
const App = () => {
return (
<Routes>
<Route path='/' element={<Navigation />} />
<Route index element={<Home />} />
<Route path='shop' element={<Shop />} />
</Routes>
);
};
export default App;
I am receiving this:
enter image description here
and I want navigation component to render all above and persist every time I navigate to elsewhere.
2
Answers
for using of Outlet you need Add children to Route Component
The
Navigation
component isn’t rendered as a layout route with nested routes, so nothing is rendered into theOutlet
it is rendering.Navigation
will also only render on path"/"
.If you want the
Navigation
component to render always then you can render it alone outside the routes.Or you can utilize it as a layout route such that it wraps nested routes that render their
element
into theOutlet
.