I am implementing react-router-dom to handle routing. The routing works for the most part however my Header
component is not visible when the user visits AccountHome
My router is very simple:
const router = createBrowserRouter([
{
path: "/",
element: <AppLayout />,
},
{ path: "/account", element: <AccountHome /> },
]);
The header is found in the AppLayout
file:
export const AppLayout = () => {
const {
isAuthenticated,
} = useContext(AppContext);
return (
<div className="App">
<Header />
{isAuthenticated ? <HomePage /> : <LandingPage />}
<Outlet />
</div>
);
};
I want the header to be visible on all pages. I feel like my implementation is close but I am misunderstanding how the Outlet works.
2
Answers
your code is simple and I guess that it should be OK but sometimes you should precision to other part of code for example you should sure about
when use createBrowserRouter and maybe in your code has problem at this section :
please check that but in finally it should be OK
The point of layout route components is for them to wrap nested routes, e.g. the nested routes render their
element
content into the layout’sOutlet
.Move the
"/account"
route to be a child of the layout route.