all exports/imports work fine.
MainLayout.js file the only file in layout :
the sidebar is fixed in all pages except Login, Register and Not Found.
in folder layout there is just MainLayout.js
const MainLayout = (props) => {
return (
<>
{/* Sidebar component is rendered on the left side */}
<Sidebar />
{/* Main content wrapped inside <main> tag */}
<main>{props.children}</main>
</>
);
};
Route.js file :
profile, profile edit ,home (all children) are not accessible
const router = createBrowserRouter([
{
path: "/login",
element: <Login />,
},
{
path: "/register",
element: <Register />,
},
{
path: "/",
element: <MainLayout />,
children: [
{
path: "/home",
element: <Home />,
},
{
path: "/profile/:profileId",
element: <Profile />,
children: [
{
path: "/profile/:profileId/edit",
element: <ProfileEdit />,
},
],
},
{
path: "/contacts/:userId",
element: <Chat />,
},
],
},
{ path: "*", element: <ErrorPage /> },
]);
const Routef = () => {
return <RouterProvider router={router} />;
};
export default Routef;
index.js file :
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Route />
</React.StrictMode>
);
2
Answers
While I am not entirely sure why this is not working. I think you should consider moving the children to the Layout file and have routes nested there.
Instead of passing a
props.children
between the<main>...</main>
tag you should pass an<Outlet/>
imported fromreact-router-dom
. This will render child route elements that you’ve added in yourchildren: [...]
array.Like this: