Dev Helpers
← Back

Route sibling chapters

Place this file at app/guide/[slug]/page.tsx in an App Router project.

Open this example in the interactive library

Source code

// app/guide/[slug]/page.tsx

import Link from "next/link";

import { notFound } from "next/navigation";

export default async function RouteSiblingChapters({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const chapters = ["start", "design", "build"];
  const i = chapters.indexOf(slug);
  if (i < 0) notFound();
  return (
    <main>
      <h1>{slug}</h1>
      {i > 0 && <Link href={"/guide/" + chapters[i - 1]}>Previous chapter </Link>}
      {i + 1 < chapters.length && (
        <Link href={"/guide/" + chapters[i + 1]}>Next chapter</Link>
      )}
    </main>
  );
}