File tree Expand file tree Collapse file tree 2 files changed +5
-25
lines changed Expand file tree Collapse file tree 2 files changed +5
-25
lines changed Original file line number Diff line number Diff line change 1
1
// 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" ;
9
3
10
4
export function LocaleLink ( {
11
5
to,
@@ -16,8 +10,7 @@ export function LocaleLink({
16
10
children : React . ReactNode ;
17
11
className ?: string ;
18
12
} ) {
19
- const rootData = useRootData ( ) ;
20
- const locale = rootData ?. locale ?? "en" ;
13
+ const { locale } = useParams ( ) ;
21
14
22
15
const path = `/${ locale } ${ to } ` ;
23
16
Original file line number Diff line number Diff line change 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" ;
8
2
9
3
// NavLinkProps を継承
10
4
type NavLocaleLinkProps = Omit < NavLinkProps , "to" > & {
@@ -17,20 +11,13 @@ export function NavLocaleLink({
17
11
children,
18
12
...rest
19
13
} : NavLocaleLinkProps ) {
20
- const rootData = useRootData ( ) ;
21
- const locale = rootData ?. locale ?? "en" ;
14
+ const { locale } = useParams ( ) ;
22
15
23
- // 先頭のスラッシュを削除したうえで "/:locale" を付与
24
16
const normalized = to . startsWith ( "/" ) ? to . slice ( 1 ) : to ;
25
17
const path = `/${ locale } /${ normalized } ` ;
26
18
27
19
return (
28
- < NavLink
29
- to = { path }
30
- // className が「文字列」or「コールバック」どちらでもOK
31
- className = { className }
32
- { ...rest }
33
- >
20
+ < NavLink to = { path } className = { className } { ...rest } >
34
21
{ children }
35
22
</ NavLink >
36
23
) ;
You can’t perform that action at this time.
0 commit comments