Skip to content

Commit 7c0760f

Browse files
authored
Replace useMatches with useParams in LocaleLink and NavLocaleLink components (#454)
2 parents 2263026 + a0c068b commit 7c0760f

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

web/app/components/LocaleLink.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// LocaleLink.tsx
2-
import { Link, useMatches } from "@remix-run/react";
3-
4-
function useRootData() {
5-
const matches = useMatches();
6-
const rootMatch = matches.find((match) => match.id === "root");
7-
return rootMatch?.data as { locale: string } | undefined;
8-
}
2+
import { Link, useParams } from "@remix-run/react";
93

104
export function LocaleLink({
115
to,
@@ -16,8 +10,7 @@ export function LocaleLink({
1610
children: React.ReactNode;
1711
className?: string;
1812
}) {
19-
const rootData = useRootData();
20-
const locale = rootData?.locale ?? "en";
13+
const { locale } = useParams();
2114

2215
const path = `/${locale}${to}`;
2316

web/app/components/NavLocaleLink.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import { NavLink, type NavLinkProps, useMatches } from "@remix-run/react";
2-
3-
function useRootData() {
4-
const matches = useMatches();
5-
const rootMatch = matches.find((match) => match.id === "root");
6-
return rootMatch?.data as { locale?: string } | undefined;
7-
}
1+
import { NavLink, type NavLinkProps, useParams } from "@remix-run/react";
82

93
// NavLinkProps を継承
104
type NavLocaleLinkProps = Omit<NavLinkProps, "to"> & {
@@ -17,20 +11,13 @@ export function NavLocaleLink({
1711
children,
1812
...rest
1913
}: NavLocaleLinkProps) {
20-
const rootData = useRootData();
21-
const locale = rootData?.locale ?? "en";
14+
const { locale } = useParams();
2215

23-
// 先頭のスラッシュを削除したうえで "/:locale" を付与
2416
const normalized = to.startsWith("/") ? to.slice(1) : to;
2517
const path = `/${locale}/${normalized}`;
2618

2719
return (
28-
<NavLink
29-
to={path}
30-
// className が「文字列」or「コールバック」どちらでもOK
31-
className={className}
32-
{...rest}
33-
>
20+
<NavLink to={path} className={className} {...rest}>
3421
{children}
3522
</NavLink>
3623
);

0 commit comments

Comments
 (0)