createRoutesFromElements
createRoutesFromElements
is a helper that creates route objects from <Route>
elements. It's useful if you prefer to create your routes as JSX instead of objects.
import { createBrowserRouter, 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: const router = createBrowserRouter([ { path: "/", element: <Root />, children: [ { path: "dashboard", element: <Dashboard />, }, { path: "about", element: <About />, }, ], }, ]);
It's also used internally by <Routes>
to generate a route objects from its <Route>
children.
declare function createRoutesFromElements( children: React.ReactNode ): RouteObject[]; interface RouteObject { caseSensitive?: boolean; children?: RouteObject[]; element?: React.ReactNode; index?: boolean; path?: string; }
© React Training 2015-2019
© Remix Software 2020-2022
Licensed under the MIT License (MIT).
https://reactrouterdotcom.fly.dev/docs/en/v6/utils/create-routes-from-elements