• Home
  • Overview
  • Layout
  • Data fetching
  • Static
  • Metadata
  • Preview
  • RSC
  • Server Actions
  • Intercepting Routes
  • API Routes
  • Fonts
  • SSR
  • Next.js Logo
    13

Intercepting routes

Docs

Glenna Reichert

EmailPhoneCompanyWebsite
Chaim_McDermott@dana.io(775)976-6794 x41206Yost and Sonsconrad.com

Help


app/intercepting-routes/posts/layout.tsx

const PostsLayout = async ({
  children,
  modal,
}: React.PropsWithChildren & { modal: React.ReactNode }) => {
  return (
    <div>
      {children}
      {modal}
    </div>
  );
};

app/intercepting-routes/users/[id]/page.tsx

const UserPage = async ({ params }: { params: Promise<{ id: string }> }) => {
  const [user] = await api.user(params.id);

  return (
    <div>
      <h2>{user.name}</h2>

      <table>
        <thead>
          <tr>
            <th>Email</th>
            <th>Phone</th>
            <th>Company</th>
            <th>Website</th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td>{user.email}</td>
            <td>{user.phone}</td>
            <td>{user.company.name}</td>
            <td>{user.website}</td>
          </tr>
        </tbody>
      </table>
    </div>
  );
};

app/intercepting-routes/posts/@modal/(..)users/[id]/page.tsx

const UserModalPage = async ({
  params,
}: {
  params: Promise<{ id: string }>;
}) => {
  const [user] = await api.user(params.id);

  return (
    <dialog open>
      <article>
        <header>
          <h2 style={{ marginBottom: 0 }}>{user.name}</h2>
        </header>

        <table>
          <thead>
            <tr>
              <th>Email</th>
              <th>Phone</th>
              <th>Company</th>
              <th>Website</th>
            </tr>
          </thead>

          <tbody>
            <tr>
              <td>{user.email}</td>
              <td>{user.phone}</td>
              <td>{user.company.name}</td>
              <td>{user.website}</td>
            </tr>
          </tbody>
        </table>

        <BackLink />
      </article>
    </dialog>
  );
};

components/BackLink.tsx

'use client';

export const BackLink = () => {
  const router = useRouter();

  return (
    <button onClick={router.back} style={{ marginTop: 20 }}>
      Go back
    </button>
  );
};