Dev Helpers
← Back

Authoritative title search

Place this file at app/books/page.tsx in an App Router project.

Open this example in the interactive library

Source code

// app/books/page.tsx

import Form from "next/form";

export default async function AuthoritativeTitleSearch({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const books = [
    { slug: "ink", name: "Print in Ink", year: 2020, genre: "Art", loans: 14 },
    { slug: "type", name: "Type Design", year: 2024, genre: "Design", loans: 8 },
    { slug: "paper", name: "Paper Works", year: 2024, genre: "Art", loans: 22 },
  ];
  const { q } = await searchParams;
  const term = typeof q === "string" ? q.trim().toLowerCase() : "";
  return (
    <main>
      <Form action="/books">
        <input aria-label="Book title" name="q" defaultValue={term} />
        <button>Find books</button>
      </Form>
      <ul>
        {books
          .filter((b) => b.name.toLowerCase().includes(term))
          .map((b) => (
            <li key={b.slug}>{b.name}</li>
          ))}
      </ul>
    </main>
  );
}