Consider this React functional component in Next.js that switches languages using the useRouter hook.
import { useRouter } from 'next/router';
export default function LanguageSwitcher() {
const router = useRouter();
const { locale, locales, pathname } = router;
return (
<div>
<p>Current locale: {locale}</p>
<ul>
{locales.map((loc) => (
<li key={loc}>
<a href={pathname} onClick={(e) => {
e.preventDefault();
router.push(pathname, pathname, { locale: loc });
}}>
Switch to {loc}
</a>
</li>
))}
</ul>
</div>
);
}Assuming the current locale is 'en' and available locales are ['en', 'fr'], what will the component display?
import { useRouter } from 'next/router'; export default function LanguageSwitcher() { const router = useRouter(); const { locale, locales, pathname } = router; return ( <div> <p>Current locale: {locale}</p> <ul> {locales.map((loc) => ( <li key={loc}> <a href={pathname} onClick={(e) => { e.preventDefault(); router.push(pathname, pathname, { locale: loc }); }}> Switch to {loc} </a> </li> ))} </ul> </div> ); }
Check the locale and locales values from the router object.
The component reads the current locale as 'en' and the available locales as ['en', 'fr']. It renders the current locale and a list of links for each locale. So it shows 'Current locale: en' and two links: 'Switch to en' and 'Switch to fr'.
In Next.js, you want to change the language without reloading the page by clicking a link. Which code snippet correctly uses router.push to change locale?
Remember to prevent default link behavior and pass the correct arguments to router.push.
Option C prevents the default link behavior and calls router.push with the current pathname for both URL and asPath, and passes the locale option correctly. Other options either miss preventing default, use wrong arguments, or incorrect method signature.
This Next.js language switcher uses anchor tags with href set to the current path and an onClick handler that calls router.push with locale. However, clicking the links causes a full page reload. Why?
const LanguageSwitcher = () => {
const router = useRouter();
return (
<ul>
{router.locales.map(loc => (
<li key={loc}>
<a href={router.pathname} onClick={() => router.push(router.pathname, router.pathname, { locale: loc })}>
{loc}
</a>
</li>
))}
</ul>
);
};const LanguageSwitcher = () => { const router = useRouter(); return ( <ul> {router.locales.map(loc => ( <li key={loc}> <a href={router.pathname} onClick={() => router.push(router.pathname, router.pathname, { locale: loc })}> {loc} </a> </li> ))} </ul> ); };
Think about default link behavior in browsers.
The anchor tag's default behavior is to navigate to the href URL, causing a full reload. Without calling e.preventDefault() in the onClick handler, the browser performs the default navigation after the router.push call.
Given this Next.js language switcher component, what will be the value of locale state after clicking the 'Switch to fr' link?
import { useRouter } from 'next/router';
import { useState } from 'react';
export default function LanguageSwitcher() {
const router = useRouter();
const [currentLocale, setCurrentLocale] = useState(router.locale);
const changeLocale = (loc) => {
router.push(router.pathname, router.pathname, { locale: loc });
setCurrentLocale(loc);
};
return (
<div>
<p>Current locale: {currentLocale}</p>
<button onClick={() => changeLocale('fr')}>Switch to fr</button>
</div>
);
}import { useRouter } from 'next/router'; import { useState } from 'react'; export default function LanguageSwitcher() { const router = useRouter(); const [currentLocale, setCurrentLocale] = useState(router.locale); const changeLocale = (loc) => { router.push(router.pathname, router.pathname, { locale: loc }); setCurrentLocale(loc); }; return ( <div> <p>Current locale: {currentLocale}</p> <button onClick={() => changeLocale('fr')}>Switch to fr</button> </div> ); }
Consider what happens to React state when Next.js changes locale via router.push.
Option B is correct. The changeLocale function calls setCurrentLocale(loc) synchronously right after router.push. Since the state setter is synchronous and router.push starts an asynchronous navigation, the currentLocale updates to 'fr' immediately after clicking.
Next.js supports internationalized routing with locale detection. Which configuration enables automatic locale detection and locale-aware routing?
Check Next.js official docs on internationalized routing configuration.
Next.js supports automatic locale detection and routing by configuring the i18n object in next.config.js with locales, defaultLocale, and localeDetection: true. This enables Next.js to detect user locale and serve the correct localized page automatically.