0
0
NextJSframework~20 mins

Language switching UI in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Language Switching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Next.js language switcher component render?

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?

NextJS
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>
  );
}
AA paragraph showing 'Current locale: en' and a list with two links: 'Switch to en' and 'Switch to fr'.
BA paragraph showing 'Current locale: en' and no links because locales is empty.
CA paragraph showing 'Current locale: en' and a list with one link: 'Switch to fr' only.
DA paragraph showing 'Current locale: fr' and a list with two links: 'Switch to en' and 'Switch to fr'.
Attempts:
2 left
💡 Hint

Check the locale and locales values from the router object.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses Next.js router to change locale on click?

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?

AonClick={e => { e.preventDefault(); router.push(router.pathname, { locale: 'fr' }); }}
BonClick={e => { e.preventDefault(); router.push('/fr'); }}
ConClick={e => { e.preventDefault(); router.push(router.pathname, router.pathname, { locale: 'fr' }); }}
DonClick={e => { router.push(router.pathname, router.pathname, { locale: 'fr' }); }}
Attempts:
2 left
💡 Hint

Remember to prevent default link behavior and pass the correct arguments to router.push.

🔧 Debug
advanced
2:00remaining
Why does this Next.js language switcher cause a full page reload?

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>
  );
};
NextJS
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>
  );
};
ABecause the href attribute is missing, so the browser reloads the page.
BBecause router.push is called with wrong arguments causing a reload fallback.
CBecause Next.js does not support locale changes via router.push.
DBecause the onClick handler does not call e.preventDefault(), so the browser follows the href and reloads the page.
Attempts:
2 left
💡 Hint

Think about default link behavior in browsers.

state_output
advanced
2:00remaining
What is the value of the locale state after clicking 'Switch to fr' link?

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>
  );
}
NextJS
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>
  );
}
AThe original locale (e.g., 'en'), because router.push triggers a page reload resetting state.
B'fr', because setCurrentLocale is called immediately after router.push.
CUndefined, because currentLocale is not updated properly.
DAn error occurs because setCurrentLocale is called inside changeLocale.
Attempts:
2 left
💡 Hint

Consider what happens to React state when Next.js changes locale via router.push.

🧠 Conceptual
expert
3:00remaining
Which Next.js feature enables automatic locale detection and routing?

Next.js supports internationalized routing with locale detection. Which configuration enables automatic locale detection and locale-aware routing?

ASetting <code>i18n.locales</code> and <code>i18n.defaultLocale</code> in <code>next.config.js</code> with <code>localeDetection: true</code>.
BUsing <code>getStaticProps</code> to fetch locale data manually for each page.
CAdding a custom server to parse Accept-Language headers and redirect accordingly.
DUsing <code>next-i18next</code> package without any Next.js config changes.
Attempts:
2 left
💡 Hint

Check Next.js official docs on internationalized routing configuration.