Skip to content

Commit

Permalink
docs: Adjust locale switcher example to provide more type safety (#1256)
Browse files Browse the repository at this point in the history
Related:
- #1240
- #853
  • Loading branch information
amannn authored Aug 14, 2024
1 parent ba067df commit aecb75c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 7 additions & 4 deletions docs/pages/docs/routing/navigation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,13 @@ const router = useRouter();
const params = useParams();

router.replace(
// @ts-expect-error -- TypeScript will validate that only known `params`
// are used in combination with a given `pathname`. Since the two will
// always match for the current route, we can skip runtime checks.
{pathname, params},
{
pathname,
// @ts-expect-error -- TypeScript will validate that only known `params`
// are used in combination with a given `pathname`. Since the two will
// always match for the current route, we can skip runtime checks.
params
},
{locale: 'de'}
);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clsx from 'clsx';
import {useParams} from 'next/navigation';
import {ChangeEvent, ReactNode, useTransition} from 'react';
import {useRouter, usePathname} from '@/navigation';
import {Locale} from '@/types';

type Props = {
children: ReactNode;
Expand All @@ -22,13 +23,16 @@ export default function LocaleSwitcherSelect({
const params = useParams();

function onSelectChange(event: ChangeEvent<HTMLSelectElement>) {
const nextLocale = event.target.value;
const nextLocale = event.target.value as Locale;
startTransition(() => {
router.replace(
// @ts-expect-error -- TypeScript will validate that only known `params`
// are used in combination with a given `pathname`. Since the two will
// always match for the current route, we can skip runtime checks.
{pathname, params},
{
pathname,
// @ts-expect-error -- TypeScript will validate that only known `params`
// are used in combination with a given `pathname`. Since the two will
// always match for the current route, we can skip runtime checks.
params
},
{locale: nextLocale}
);
});
Expand Down
3 changes: 3 additions & 0 deletions examples/example-app-router/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {locales} from './config';

export type Locale = (typeof locales)[number];

0 comments on commit aecb75c

Please sign in to comment.