Skip to content

Instantly share code, notes, and snippets.

@treckstar
Created April 5, 2023 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treckstar/33524a130b2d70c23418a21960732156 to your computer and use it in GitHub Desktop.
Save treckstar/33524a130b2d70c23418a21960732156 to your computer and use it in GitHub Desktop.
Class Based TypeScript React.Component - Locale Switcher Menu
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