0
0
NextJSframework~20 mins

Programmatic navigation (useRouter) in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
  );
}
AThe page reloads completely and then goes to '/dashboard'.
BThe page navigates to '/dashboard' without a full reload.
CNothing happens because useRouter is not imported correctly.
DAn error occurs because router.push requires a second argument.
Attempts:
2 left
💡 Hint
Think about how Next.js handles client-side navigation with useRouter.
📝 Syntax
intermediate
2: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>;
}
Arouter.replace('/profile');
Brouter.navigate('/profile', { replace: true });
Crouter.push('/profile', { replace: true });
Drouter.replacePage('/profile');
Attempts:
2 left
💡 Hint
Check the method name and parameters for replacing history entries.
🔧 Debug
advanced
2: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>;
}
AuseRouter is imported from 'next/router' instead of 'next/navigation', causing a runtime error in Next.js 14+.
Brouter.push requires an async function, so the arrow function must be async.
CThe button must be wrapped in a <Link> component for navigation to work.
Drouter.push('/home') is missing a second argument for query parameters.
Attempts:
2 left
💡 Hint
Check the import source of useRouter in Next.js 14+.
state_output
advanced
2: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>;
}
AThe URL does not change because query parameters are not supported in router.push.
BThe URL changes to '/search' and query parameters are lost.
CThe URL changes to '/search?query=books&page=2' but causes a full page reload.
DThe URL changes to '/search?query=books&page=2' with no page reload.
Attempts:
2 left
💡 Hint
Consider how router.push handles query strings in URLs.
🧠 Conceptual
expert
3: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.
ABecause 'next/navigation' automatically prefetches all routes without manual calls.
BBecause 'next/router' is deprecated and no longer works in any Next.js version.
CBecause 'next/navigation' provides a new hook designed for React Server Components and client components with improved performance and simpler API.
DBecause 'next/router' only works with class components, which are no longer supported.
Attempts:
2 left
💡 Hint
Think about Next.js architecture changes and React Server Components.