Discover how to make your Next.js app feel lightning fast by navigating pages without reloads!
Why Programmatic navigation (useRouter) in NextJS? - Purpose & Use Cases
Imagine building a website where you want to move users to different pages after they click buttons or complete forms, but you have to reload the whole page every time.
Manually reloading pages or using plain links breaks the smooth flow of your app, causes delays, and can lose user data or state. It feels clunky and slow, like flipping through a book by tearing out pages.
Next.js's useRouter hook lets you change pages instantly in your app without full reloads, keeping everything fast and smooth like turning pages in a well-made digital book.
window.location.href = '/dashboard';import { useRouter } from 'next/router'; const router = useRouter(); router.push('/dashboard');
This lets your app feel fast and responsive, improving user experience by navigating pages seamlessly without losing app state.
After a user logs in, you want to send them to their profile page immediately without refreshing the whole site, keeping their data and app state intact.
Manual page reloads are slow and disrupt user flow.
useRouter enables smooth, instant navigation inside Next.js apps.
It improves speed, user experience, and keeps app state intact during navigation.
Practice
useRouter hook from next/navigation allow you to do in a Next.js app?Solution
Step 1: Understand the purpose of useRouter
TheuseRouterhook provides methods to change pages in code, not by user clicking links.Step 2: Identify what useRouter does not do
It does not handle data fetching, styling, or auto-reloading pages.Final Answer:
Navigate between pages programmatically using code -> Option AQuick Check:
useRouter = programmatic navigation [OK]
- Confusing useRouter with data fetching hooks
- Thinking useRouter styles components
- Assuming useRouter reloads pages automatically
useRouter from Next.js 14+ for programmatic navigation?Solution
Step 1: Recall the correct import path for useRouter in Next.js 14+
In Next.js 14+,useRouteris imported fromnext/navigation, notnext/router.Step 2: Check syntax correctness
import { useRouter } from 'next/navigation'; uses named import from the correct module. import useRouter from 'next/navigation'; misses braces, causing syntax error. import { useRouter } from 'next/navigation/useRouter'; uses a wrong path.Final Answer:
import { useRouter } from 'next/navigation'; -> Option AQuick Check:
Correct import = import { useRouter } from 'next/navigation'; [OK]
- Importing from 'next/router' instead of 'next/navigation'
- Missing curly braces in import
- Using incorrect import paths
import { useRouter } from 'next/navigation';
export default function Home() {
const router = useRouter();
return (
<button onClick={() => router.push('/about')}>Go to About</button>
);
}Solution
Step 1: Understand router.push behavior
Therouter.push('/about')method navigates to the '/about' page and adds a new entry in the browser history stack.Step 2: Confirm the code triggers navigation on button click
The button's onClick calls router.push correctly, so navigation happens as expected.Final Answer:
The page will navigate to '/about' adding a new entry in browser history -> Option CQuick Check:
router.push adds history entry = The page will navigate to '/about' adding a new entry in browser history [OK]
- Confusing router.push with router.replace
- Thinking router.push reloads current page
- Assuming router.push does nothing without await
useRouter for navigation:import { useRouter } from 'next/navigation';
export default function Page() {
const router = useRouter;
return (
<button onClick={() => router.push('/contact')}>Contact</button>
);
}Solution
Step 1: Check how useRouter is assigned
The code assignsconst router = useRouter;without calling it as a function. It should beuseRouter().Step 2: Verify other parts
router.push is valid for navigation. onClick syntax is correct. Import path is correct.Final Answer:
Missing parentheses when calling useRouter hook -> Option DQuick Check:
useRouter must be called as a function [OK]
- Assigning useRouter without parentheses
- Using router.push incorrectly in onClick
- Importing useRouter from wrong module
useRouter should you use and how?const router = useRouter(); // Your code here
Solution
Step 1: Understand difference between push and replace
router.push()adds a new entry to browser history, whilerouter.replace()swaps the current entry.Step 2: Choose method to avoid adding history entry
To navigate without adding a new history entry, userouter.replace('/newpage').Final Answer:
Use router.replace('/newpage') to replace current history entry -> Option BQuick Check:
replace swaps history entry = Userouter.replace('/newpage')to replace current history entry [OK]
- Using router.push when replace is needed
- Confusing reload with navigation
- Using router.back instead of forward navigation
Ready to prove it?
Pass the test and this pattern moves unknown
