gpbaculio
gpbaculio

Reputation: 5968

how to make path for / and home in react router v6?

this is my routes now, which don't work:

const routes = [
  {
    path: "/",
    element: <Navbar />,
    children: [{ path: "/(home)?", element: <Home /> }],
  },
];

i remember working on path before for home like this:

<Route path="/(home)?" element={<Home />} />

i want to match / and /home and render Home component for both?

Upvotes: 0

Views: 743

Answers (1)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11905

You can pass an array of children routes for / and /home.

const routes = [
  {
    path: '/',
    element: <Navbar />,
    children: [
      { path: '/', element: <Home /> },
      { path: 'home', element: <Home /> },
    ],
  },
]

Note that the Navbar component should render Outlet to render the child routes.

import { Outlet } from 'react-router-dom'

const Navbar = () => (
  <>
    <h1>Navbar</h1>
    <Outlet />
  </>
)

Edit reverent-ellis-cn8j5

Upvotes: 3

Related Questions