Skip to content

Commit

Permalink
Fix useMatches
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 1, 2022
1 parent 502ac8d commit 7dd22ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions packages/remix-react/components.tsx
Expand Up @@ -29,6 +29,7 @@ import {
useFetcher as useFetcherRR,
useActionData as useActionDataRR,
useLoaderData as useLoaderDataRR,
useMatches as useMatchesRR,
useLocation,
useNavigation,
useHref,
Expand Down Expand Up @@ -874,7 +875,7 @@ export function useBeforeUnload(
}, [callback]);
}

// TODO: Handle this typing
// TODO: Can this be re-exported from RR?
export interface RouteMatch {
/**
* The id of the matched route
Expand All @@ -893,7 +894,7 @@ export interface RouteMatch {
/**
* Any route data associated with the matched route
*/
data: RouteData;
data: any;
/**
* The exported `handle` object of the matched route.
*
Expand All @@ -902,6 +903,23 @@ export interface RouteMatch {
handle: undefined | { [key: string]: any };
}

export function useMatches(): RouteMatch[] {
let { routeModules } = useRemixContext();
let matches = useMatchesRR();
return matches.map((match) => {
let remixMatch: RouteMatch = {
id: match.id,
pathname: match.pathname,
params: match.params,
data: match.data,
// Need to grab handle here since we don't have it at client-side route
// creation time
handle: routeModules[match.id].handle,
};
return remixMatch;
});
}

/**
* Returns the JSON parsed data from the current route's `loader`.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/index.tsx
Expand Up @@ -16,7 +16,6 @@ export {
useFormAction,
useHref,
useLocation,
useMatches,
useNavigate,
useNavigationType,
useOutlet,
Expand Down Expand Up @@ -44,6 +43,7 @@ export {
useTransition,
useFetcher,
useLoaderData,
useMatches,
useActionData,
useBeforeUnload,
} from "./components";
Expand Down

0 comments on commit 7dd22ea

Please sign in to comment.