A drop-in replacement for react-router-dom
that provides nested prefix routing.
import {
+ PrefixedBrowserRouter,
BrowserRouter,
Routes,
Route,
-} from "react-router-dom";
+} from "react-router-dom-with-prefix";
PrefixedBrowserRouter
is a provider that provides route prefix to the router components and functions wrapped within it.
<BrowserRouter basename="/prefix-1">
<Routes>
<Route path="/" element={<Outlet />}>
<Route
path="/prefix-a/*"
element={
<PrefixedBrowserRouter basename="/prefix-a">
<Routes>
{/* navigation inside PrefixedBrowserRouter are prefixed with "/prefix-a" */}
<Route path="/nested-1" element={<Link to="/nested-2">Go 2</Link>} />
<Route path="/nested-2" element={<Link to="/nested-1">Go 1</Link>} />
</Routes>
</PrefixedBrowserRouter>
}
/>
<Route path="/prefix-b" element={<PageB />} />
</Route>
</Routes>
</BrowserRouter>
The following exports are augmented with prefix awared logic
-
createPath
-
useBlocker
-
useHref
-
useLinkClickHandler
-
useLocation
-
useMatch
-
useNavigate
-
useResolvedPath
-
useRoutes
-
Link
-
NavLink
-
Navigate
-
useMatches
-
useSubmit
Nesting of BrowserRouter is no longer support since v6.
The following known exports are not augmented
useNavigationType
generatePath
matchPath
matchRoutes
parsePath
useRevalidator
useParams
resolvePath
useBeforeUnload
useInRouterContext
useOutlet
useOutletContext
useSearchParams
useActionData
useNavigation
useLoaderData
useRouteLoaderData
useRouteError
useFetcher
useFetchers
useFormAction
Await
useAsyncError
useAsyncValue
redirect
redirectDocument
renderMatches