# React18 - Routing 11 - Layout Routes - Auth'd login loses NavBar

**URL:** <https://forum.codewithmosh.com/t/react18-routing-11-layout-routes-authd-login-loses-navbar/20488>\
**Category:** React\
**Created:** [May 26, 2023, 11:25pm UTC](https://forum.codewithmosh.com/t/react18-routing-11-layout-routes-authd-login-loses-navbar/20488 "2023-05-26T23:25:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djb160](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/djb160/32/4881_2.png) [@djb160](https://forum.codewithmosh.com/u/djb160)\
**Post date:** [May 26, 2023, 11:25pm UTC](https://forum.codewithmosh.com/t/react18-routing-11-layout-routes-authd-login-loses-navbar/20488/1 "2023-05-26T23:25:47Z")

</div>

I notice after refactoring the auth to use the Layouts and PrivateRoutes solution that when you switch back to an Authenticated user, the Routing takes you to the login page but drops the NavBar.

I added back Layout under PrivateRoutes and as a parent of UsersPage, and it seems to work, but is this a correct solution?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/a/af47e2e076975a058608f4a34493dba4e63e3fe4.png)

P.S. yes I’ve spotted and corrected my ‘PrivatRoutes.tsx’ typo

---

<div class="post-metadata">

**Author:** ![ZwierQ](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/zwierq/32/6465_2.png) [@ZwierQ](https://forum.codewithmosh.com/u/ZwierQ)\
**Post date:** [June 13, 2023, 10:11pm UTC](https://forum.codewithmosh.com/t/react18-routing-11-layout-routes-authd-login-loses-navbar/20488/2 "2023-06-13T22:11:27Z")

</div>

I noticed the same problem and at first did exactly the same. But I wondered how can I do this without repeating same code in two places and just passed PrivateRoutes component as a child of Layout, because that’s the place we have NavBar component.

```typescript
import { createBrowserRouter } from "react-router-dom";
import HomePage from "./HomePage";
import UserDetail from "./UserDetail";
import Layout from "./Layout";
import UsersPage from "./UsersPage";
import ErrorPage from "./ErrorPage";
import LoginPage from "./LoginPage";
import PrivateRoutes from "./PrivateRoutes";

const router = createBrowserRouter([
    {
        path: "/",
        element: <Layout />,
        errorElement: <ErrorPage />,
        children: [
            { index: true, element: <HomePage /> },
            { path: "/login", element: <LoginPage /> },
            {
                element: <PrivateRoutes />,
                children: [
                    {
                        path: "users",
                        element: <UsersPage />,
                        children: [{ path: ":id", element: <UserDetail /> }],
                    },
                ],
            },
        ],
    },
]);

export default router;

```

You can also try to use createRoutesFromElements(). For me it’s easier to visualize what’s going on with routing using JSX instead of objects. Code below:

```typescript
import {
    Route,
    createBrowserRouter,
    createRoutesFromElements,
} from "react-router-dom";
import HomePage from "./HomePage";
import UserDetail from "./UserDetail";
import Layout from "./Layout";
import UsersPage from "./UsersPage";
import ErrorPage from "./ErrorPage";
import LoginPage from "./LoginPage";
import PrivateRoutes from "./PrivateRoutes";

const router = createBrowserRouter(
    createRoutesFromElements(
        <Route path="/" element={<Layout />} errorElement={<ErrorPage />}>
            <Route index element={<HomePage />} />
            <Route path="login" element={<LoginPage />} />
            <Route element={<PrivateRoutes />}>
                <Route path="users" element={<UsersPage />}>
                    <Route path=":id" element={<UserDetail />} />
                </Route>
            </Route>
        </Route>
    )
);

export default router;

```

---

<div class="post-metadata">

**Author:** ![chumps](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@chumps](https://forum.codewithmosh.com/u/chumps)\
**Post date:** [November 1, 2023, 7:34pm UTC](https://forum.codewithmosh.com/t/react18-routing-11-layout-routes-authd-login-loses-navbar/20488/3 "2023-11-01T19:34:11Z")

</div>

My solution was to bring the `PrivateRoutes` as a child of the main (“/”) route/element/path. That way it’s rendered under the `Layout` component and with access to the `errorElement` component - Without introducing any duplication. That said, I am unsure if this is good or bad practice. Would be great to get input on this approach from anyone with good React Router knowledge.

```auto
const router = createBrowserRouter([
  {
    path: "/",
    element: <Layout />,
    errorElement: <ErrorPage />,
    children: [
      { index: true, element: <HomePage /> },
      { path: "contact", element: <ContactPage /> },
      { path: "login", element: <LoginPage /> },
      {
        element: <PrivateRoutes />,
        children: [
          {
            path: "users",
            element: <UsersPage />,
            children: [{ path: ":id", element: <UserDetail /> }],
          },
        ],
      },
    ],
  },
]);

```
