Created
April 5, 2023 15:04
-
-
Save treckstar/33524a130b2d70c23418a21960732156 to your computer and use it in GitHub Desktop.
Class Based TypeScript React.Component - Locale Switcher Menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Link from 'next/link'; | |
import { useRouter } from 'next/router'; | |
type Locale = { | |
locale: string; | |
label: string; | |
}; | |
type Props = { | |
locales: Locale[]; | |
}; | |
class LocaleMenu extends React.Component<Props> { | |
render() { | |
const { locales } = this.props; | |
const router = useRouter(); | |
const { pathname, query } = router; | |
return ( | |
<nav> | |
<ul> | |
{locales.map(locale => ( | |
<li key={locale.locale}> | |
<Link href={{ pathname, query }} locale={locale.locale}> | |
<a className={locale.locale === router.locale ? 'active' : ''}>{locale.label}</a> | |
</Link> | |
</li> | |
))} | |
</ul> | |
</nav> | |
); | |
} | |
} | |
export default LocaleMenu; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment