# Client Routing (useRouter)

> Programmatic navigation and history manipulation.

Subject: Next.js | Lesson 38 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-router
Previous: [Server Actions Security](https://codewithanimation.in/nextjs/actions-security.md) | Next: [Active Links (usePathname)](https://codewithanimation.in/nextjs/use-pathname.md)

## Imperative Navigation

`useRouter` is a Client Component hook that lets you programmatically change routes.

While `<Link>` is always preferred for accessibility and SEO, `useRouter` is necessary when navigation happens as a result of an action (like a successful form submission or clicking a generic div).

## History Control

The router gives you precise control over the browser's history stack.

You can `push` a new URL onto the stack, or `replace` the current URL so the user cannot click the 'Back' button to return to the previous page (crucial for authentication flows).

## The Big Four Methods

`router.push('/url')`: Navigates to a new URL and adds it to the history stack.

`router.replace('/url')`: Navigates to a new URL but overwrites the current history entry.

`router.back()`: Navigates back one step in the browser history.

`router.refresh()`: Re-fetches the current route's Server Components from the server, without losing Client Component state (like scroll position or input values).

## Common mistake: The Import Trap

When typing `useRouter`, your IDE might auto-import it from `next/router`. **This will crash your App Router application.**

The App Router requires you to import from `next/navigation`. The old `next/router` is strictly for the legacy Pages Router.

## Tip: Don't use it for prefetching

Unlike `<Link>`, `router.push()` does NOT prefetch the route automatically.

If you need to prefetch programmatically before the user navigates, you can call `router.prefetch('/url')`.

## Try it: The History Stack

Interact with the routing methods below to see exactly how they manipulate the browser's internal History Stack.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-router

## Check yourself

### Question 1

What happens if you import `useRouter` from `next/router` in an App Router component?

- A. It works normally, but warns in the console.
- B. It throws an error and crashes the application.
- C. It disables Client-side transitions.
- D. It automatically falls back to `next/navigation`.

Correct answer: B

`next/router` is the legacy router. For the App Router, you must ALWAYS import from `next/navigation`.

- Why not A: It hard crashes.
- Why not C: It doesn't even run.
- Why not D: No auto-fallback.

### Question 2

When should you use `router.replace()` instead of `router.push()`?

- A. When you want to navigate faster.
- B. When you want to preserve the browser history stack.
- C. When you want to prevent the user from clicking 'Back' to return to the current page (e.g., after logging in).
- D. When navigating to external URLs.

Correct answer: C

`replace` overwrites the current entry in the history stack. If a user logs in and is redirected to `/dashboard` via `replace`, clicking 'Back' won't take them to the login page again.

- Why not A: Speed is the same.
- Why not B: It overwrites it.
- Why not D: useRouter is only for internal routing.

## Remember this

- Always import `useRouter` from `next/navigation`, NEVER `next/router`.
- `push()` adds a new entry to the browser's history stack.
- `replace()` overwrites the current entry in the history stack.
- `refresh()` refetches Server Components without resetting Client state.
- Use `<Link>` for declarative navigation, and `useRouter` for programmatic navigation.
