Skip to content

Instantly share code, notes, and snippets.

@edoves
Created October 30, 2024 15:16
Show Gist options
  • Save edoves/aadd4735c0dd140c802919bc19f321da to your computer and use it in GitHub Desktop.
Save edoves/aadd4735c0dd140c802919bc19f321da to your computer and use it in GitHub Desktop.
createRoutesFromElements

createRoutesFromElements.js

import {
  createBrowserRouter,
  createRoutesFromElements,
  RouterProvider,
} from "react-router-dom";

// You can do this:
const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<Root />}>
      <Route path="dashboard" element={<Dashboard />} />
      <Route path="about" element={<About />} />
    </Route>
  )
);

// Instead of this:

function Router() {
  const router = createBrowserRouter(
    createRoutesFromElements(
      <Route path="/" element={<App />}>
        <Route path="dashboard" element={<Dashboard />} />
        <Route path="about" element={<About />} />
      </Route>
    )
  )

  return <RouterProvider router={router} />
}

export default Router


// then call Router in main.jsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment