Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NNext.js

    Next.js foundations

    • What is Next.js?
    • The App Router: folders are routes
    • Dynamic routes
    • Client-side navigation

    Server, client and data

    • Server and Client Components
    • Fetching data on the server
    • loading, error and not-found
    • Caching and revalidation

    Mutations, APIs and security

    • Server Actions
    • Route Handlers: building an API
    • Proxy (formerly Middleware)
    • Authentication and protecting pages

    Going to production

    • Metadata and SEO
    • Images, fonts and performance
    • Environment variables and deploying to Vercel
    • Project: a course catalogue
    Next.js

    Next.js foundations · Lesson 4 of 16

    Client-side navigation

    usePathname, useRouter and useSearchParams, and an active navbar link.

    8 minutes2 games

    Inside client components, next/navigation gives you hooks:

    • usePathname(): the current path (/courses/react).
    • useSearchParams(): the query (?tab=lessons).
    • useRouter(): navigate in code: router.push("/dashboard"), router.back(), router.refresh().

    On the server, use redirect("/login") instead.

    TypeScript
    "use client"
    import Link from "next/link"
    import { usePathname } from "next/navigation"
    
    const LINKS = [
      { href: "/dashboard", label: "Dashboard" },
      { href: "/courses", label: "Courses" },
      { href: "/settings", label: "Settings" },
    ]
    
    export function NavBar() {
      const pathname = usePathname()
      return (
        <nav>
          {LINKS.map((l) => {
            const active = pathname === l.href || pathname.startsWith(l.href + "/")
            return (
              <Link key={l.href} href={l.href} aria-current={active ? "page" : undefined}
                className={active ? "font-bold underline" : ""}>
                {l.label}
              </Link>
            )
          })}
        </nav>
      )
    }

    The logic is plain React, so try it here with pathname passed as a prop instead of usePathname:

    React
    const LINKS = [
      { href: "/dashboard", label: "Dashboard" },
      { href: "/courses", label: "Courses" },
      { href: "/settings", label: "Settings" },
    ]
    
    function NavBar({ pathname }) {
      return (
        <nav style={{ display: "flex", gap: 12 }}>
          {LINKS.map((l) => {
            const active = pathname === l.href || pathname.startsWith(l.href + "/")
            return <a key={l.href} href="#" style={{ fontWeight: active ? 700 : 400, textDecoration: active ? "underline" : "none" }}>{l.label}</a>
          })}
        </nav>
      )
    }
    
    function App() {
      const [path, setPath] = useState("/courses/react")
      return (
        <div>
          <input value={path} onChange={(e) => setPath(e.target.value)} />
          <NavBar pathname={path} />
        </div>
      )
    }

    Important

    Using useSearchParams in a static page makes that part render only in the browser, so wrap the component using it in <Suspense> or the build fails. And don't use router.push for ordinary links: <Link> is better for accessibility (opens in a new tab, gets prefetched).

    Complete the code

    Complete it to send the user to the dashboard after login.

    "use client"
    import {  } from "next/navigation"
    
    export function LoginButton() {
      const router = useRouter()
      return <button onClick={async () => { await signIn(); router.("/dashboard") }}>Log in</button>
    }

    Interview question

    How do redirect(), router.push() and <Link> differ?

    Exercise

    Finish NavBar: the active link (when pathname equals it or starts with it + /) renders as [Courses], the others without brackets. The page uses /courses/react.

    The page should show: Dashboard [Courses] Settings

    React
    const LINKS = [
      { href: "/dashboard", label: "Dashboard" },
      { href: "/courses", label: "Courses" },
      { href: "/settings", label: "Settings" },
    ]
    
    function NavBar({ pathname }) {
      return <nav>{LINKS.map((l) => <span key={l.href}>{l.label} </span>)}</nav>
    }
    
    function App() {
      return <NavBar pathname="/courses/react" />
    }

    Learn more from

    • Next.js docs: usePathnamenextjs.org
    • Next.js docs: useRouternextjs.org
    • Next.js docs: redirectnextjs.org
    • MDN: aria-currentdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousDynamic routesNextServer and Client Components
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.