Sikau

Next.js · Day 11 · about 30 min

Search params

Filters and search in the URL.

Shareable filters

Put filters in the URL — /blog?tag=recipe — and they survive refreshes, work with the back button and can be shared. Pages receive them as search params.

searchParams on the server, links to change them

import Link from "next/link";
import { getPosts } from "@/lib/posts";

export default async function Blog({ searchParams }) {
  const { tag } = await searchParams;
  const posts = await getPosts({ tag });
  return (
    <>
      <nav>
        <Link href="/blog">All</Link> · <Link href="/blog?tag=recipe">Recipes</Link> · <Link href="/blog?tag=news">News</Link>
      </nav>
      <ul>{posts.map((p) => <li key={p.slug}>{p.title}</li>)}</ul>
    </>
  );
}
  • In Client Components, read them with useSearchParams(); update with router.push("?q=…").
  • Reading searchParams makes the page render per request (it depends on the URL).
  • Validate values: anything can be typed into a URL.

The rest of this lesson

  • The URL as stateexplain
  • Updating params from the clientexplain
  • What's tag?predict
  • Building paramspredict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 19 lessons of Next.js