Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer useMatchesData type using generic #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type SerializeFrom } from '@remix-run/node'
import { useMatches } from "@remix-run/react";
import { useMemo } from "react";

Expand Down Expand Up @@ -33,17 +34,18 @@ export function safeRedirect(
* @param {string} id The route id
* @returns {JSON|undefined} The router data or undefined if not found
*/
export function useMatchesData(
id: string,
): Record<string, unknown> | undefined {
const matchingRoutes = useMatches();
const route = useMemo(
() => matchingRoutes.find((route) => route.id === id),
[matchingRoutes, id],
);
return route?.data as Record<string, unknown>;
export function useMatchesData<T = Record<string, unknown> | undefined>(
id: string,
): SerializeFrom<T> {
const matchingRoutes = useMatches()
const route = useMemo(
() => matchingRoutes.find(route => route.id === id),
[matchingRoutes, id],
)
return route?.data as SerializeFrom<T>
}


function isUser(user: unknown): user is User {
return (
user != null &&
Expand Down