Challenge - 5 Problems
Next.js Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when this Next.js component runs?
Consider this Next.js functional component using
useRouter. What will the button do when clicked?NextJS
import { useRouter } from 'next/navigation'; export default function NavigateButton() { const router = useRouter(); return ( <button onClick={() => router.push('/dashboard')}>Go to Dashboard</button> ); }
Attempts:
2 left
💡 Hint
Think about how Next.js handles client-side navigation with useRouter.
✗ Incorrect
The useRouter hook from 'next/navigation' enables client-side navigation. Calling router.push('/dashboard') changes the URL and renders the new page without a full reload.
📝 Syntax
intermediate2:00remaining
Which option correctly uses useRouter to replace the current page?
You want to replace the current page in history with '/profile' using useRouter. Which code snippet is correct?
NextJS
import { useRouter } from 'next/navigation'; export default function ReplacePage() { const router = useRouter(); function handleReplace() { // Your code here } return <button onClick={handleReplace}>Replace with Profile</button>; }
Attempts:
2 left
💡 Hint
Check the method name and parameters for replacing history entries.
✗ Incorrect
The useRouter hook provides a replace() method to replace the current history entry. The push() method does not accept a replace option. navigate() and replacePage() do not exist.
🔧 Debug
advanced2:00remaining
Why does this code cause a runtime error?
This Next.js component throws an error when clicking the button. What is the cause?
NextJS
import { useRouter } from 'next/router'; export default function ErrorButton() { const router = useRouter(); return <button onClick={() => router.push('/home')}>Go Home</button>; }
Attempts:
2 left
💡 Hint
Check the import source of useRouter in Next.js 14+.
✗ Incorrect
In Next.js 14+, useRouter for client navigation is imported from 'next/navigation'. Importing from 'next/router' causes runtime errors because that package is deprecated for client components.
❓ state_output
advanced2:00remaining
What is the URL after this code runs?
Given this component, what will be the URL after clicking the button?
NextJS
import { useRouter } from 'next/navigation'; export default function QueryNav() { const router = useRouter(); function goToSearch() { router.push('/search?query=books&page=2'); } return <button onClick={goToSearch}>Search Books</button>; }
Attempts:
2 left
💡 Hint
Consider how router.push handles query strings in URLs.
✗ Incorrect
router.push accepts full URLs including query strings and performs client-side navigation without reload, updating the URL accordingly.
🧠 Conceptual
expert3:00remaining
Why use useRouter from 'next/navigation' instead of 'next/router' in Next.js 14+?
Select the best explanation for preferring useRouter from 'next/navigation' in modern Next.js apps.
Attempts:
2 left
💡 Hint
Think about Next.js architecture changes and React Server Components.
✗ Incorrect
'next/navigation' is the modern routing API optimized for React Server Components and client components, offering better integration and performance. 'next/router' is legacy and not recommended for new apps.