Skip to content

Commit

Permalink
Clean up useRoutes RouterProvider detection (#10389)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Apr 24, 2023
1 parent 3efa5d0 commit 7110596
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
useOutlet,
useRoutes,
_renderMatches,
useRoutesImpl,
} from "./hooks";

export interface RouterProviderProps {
Expand Down Expand Up @@ -132,7 +133,9 @@ function DataRoutes({
}: {
routes: DataRouteObject[];
}): React.ReactElement | null {
return useRoutes(routes);
let state = React.useContext(DataRouterStateContext);
invariant(state, "No Router state available for DataRoutes");
return useRoutesImpl(routes, undefined, state);
}

export interface MemoryRouterProps {
Expand Down
16 changes: 10 additions & 6 deletions packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ export function useResolvedPath(
export function useRoutes(
routes: RouteObject[],
locationArg?: Partial<Location> | string
): React.ReactElement | null {
return useRoutesImpl(routes, locationArg);
}

// Internal implementation with accept optional param for RouterProvider usage
export function useRoutesImpl(
routes: RouteObject[],
locationArg?: Partial<Location> | string,
dataRouterState?: RemixRouter["state"]
): React.ReactElement | null {
invariant(
useInRouterContext(),
Expand All @@ -320,8 +329,6 @@ export function useRoutes(
);

let { navigator } = React.useContext(NavigationContext);
let dataRouterContext = React.useContext(DataRouterContext);
let dataRouterStateContext = React.useContext(DataRouterStateContext);
let { matches: parentMatches } = React.useContext(RouteContext);
let routeMatch = parentMatches[parentMatches.length - 1];
let parentParams = routeMatch ? routeMatch.params : {};
Expand Down Expand Up @@ -434,10 +441,7 @@ export function useRoutes(
})
),
parentMatches,
// Only pass along the dataRouterStateContext when we're rendering from the
// RouterProvider layer. If routes is different then we're rendering from
// a descendant <Routes> tree
dataRouterContext?.router.routes === routes ? dataRouterStateContext : null
dataRouterState
);

// When a user passes in a `locationArg`, the associated routes need to
Expand Down

0 comments on commit 7110596

Please sign in to comment.