Complete the code to import the Next.js router hook for navigation.
import { [1] } from 'next/navigation';
useState for routingThe useRouter hook from next/navigation is used to access routing functions in Next.js.
Complete the code to get the router object inside the component.
const router = [1]();useState instead of useRouterCalling useRouter() inside a component returns the router object for navigation.
Fix the error in the code to change the locale to 'fr' using the router.
router.[1]({ locale: 'fr' });
reload which reloads the pageprefetch which only preloads dataThe push method navigates to a new URL or changes locale without reloading the page.
Fill both blanks to create a button that switches language to Spanish ('es') on click.
const switchToSpanish = () => {
router.[1]({ locale: '[2]' });
};replace which replaces history entryUse router.push to change the locale. The locale code for Spanish is 'es'.
Fill all three blanks to create a language switcher component that updates locale and shows current language.
import { useState } from 'react'; import { [1] } from 'next/navigation'; export default function LanguageSwitcher() { const router = [2](); const [lang, setLang] = useState('en'); const changeLanguage = (newLang) => { router.[3]({ locale: newLang }); setLang(newLang); }; return ( <button onClick={() => changeLanguage(lang === 'en' ? 'fr' : 'en')} aria-label="Switch language"> Current: {lang.toUpperCase()} </button> ); }
replace instead of pushImport and call useRouter to get the router. Use router.push to change locale. This updates the language and UI.