WhoBuilt

Navigating Between Pages

Jul 3, 2020· 19 min· 26K views·Watch on YouTube

Learn Next.js on the leerob channel explains how to navigate between pages in a Next.js app, with examples covering client-side links, dynamic routes, data fetching, and login redirects.

Client-side links

Learn Next.js introduces the Next.js Link component as a way to navigate between pages without a full page reload. Links use an `href` and should have an `a` tag as their first child. Next.js also prefetches linked pages, which can make navigation feel faster.

Dynamic routes

The example creates a `post/[someId]` page, where the bracketed segment marks a variable route value. The `useRouter` hook exposes that value through `router.query`. Links can point to `/post/[id]` and use `as` to show a specific path, such as `/post/first-post`.

Fetching data for a post

Learn Next.js uses `getInitialProps` to fetch a post from JSONPlaceholder before rendering the page. The route ID selects the post, and the component displays its title and body. The same approach can call other APIs, including a project's own API.

Redirecting to a dashboard

The login example uses `useRouter` and `router.push` to send the user to a dashboard after clicking the login button. The dashboard fetches profile details from the GitHub API with `getInitialProps` and renders fields such as name, bio, and blog. The demo shows the redirect and data fetch, but does not implement authentication.

Key navigation patterns

The module closes by recapping client-side navigation with Link, dynamic routes, redirects with `router.push`, and page data fetching with `getInitialProps`. Together, these examples show how routes and API data can shape the pages in a Next.js app.

Takeaways

  • Use Next.js Link for client-side page navigation and prefetching.
  • Represent variable path segments with bracketed page names and read their values from `router.query`.
  • Use `getInitialProps` in the demonstrated pattern to fetch route-specific API data before rendering.
  • Use `router.push` to redirect after a client-side action; add real authentication checks separately.

Mentioned in this talk

Search inside this talk

What was claimed 5

  • feature
    Next.js: prefetches information for client-side links on a page
    prefetches information for client-side links on a page
    “Next.js determines which client-side Links are on the page and prefetches the information”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: fetches data before the component renders
    fetches data before the component renders
    “allows us to fetch data before the component renders”
    YouTube
    Unverified1 source· Talk
  • feature
    JSONPlaceholder: Provides dummy post data through an API
    “a free API that gives us dummy post data”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: supports programmatic navigation to another page
    supports programmatic navigation to another page
    “Do router.push, and we want to redirect to this dashboard page”
    YouTube
    Unverified1 source· Talk
  • uses
    “We're going to use the same approach we used for the posts, which is getInitialProps”
    YouTube
    Unverified1 source· Talk

Related talks

Navigating Between Pages · WhoBuilt