← Back to React, page 5

react-0081 · Medium mid

What are the core ideas of the Next.js App Router (file conventions, Server and Client Components, loading and error files, Server Actions)? This question is specific to the Next.js framework.

This is framework-specific, not core React. In the App Router, folders define routes and special files define behaviour: page, layout, loading, error and not-found. Components are Server Components by default, can be async and fetch data directly, and ship no JavaScript. Add "use client" to a file for state, effects and browser APIs. Server Actions ("use server" functions) handle mutations from forms. In current versions params is a Promise to await.

Full answer

Next.js is a framework built on React, and the App Router (the app directory) is its routing model based on React Server Components. Interviewers ask about it because many React jobs are Next.js jobs, but remember to separate framework features from React itself.

Routing by files. Each folder is a URL segment, and a page file makes it public: app/courses/[id]/page.tsx serves /courses/123. layout wraps a segment and its children and persists between navigations. loading is shown through Suspense while the segment loads. error is an error boundary for the segment (it must be a Client Component). not-found renders for missing content. route files define API handlers. Dynamic segments use brackets, and groups in parentheses organize files without changing the URL.

Server and Client Components. Files are Server Components by default: they run on the server, can be async, can read a database or call an API directly, and their code does not reach the browser. A file starting with "use client" marks a boundary: that module and what it imports run in the browser too, which is needed for useState, useEffect, event handlers and browser APIs. Keep the boundary low in the tree, so most of the page stays on the server. Server Components can render Client Components and pass them serializable props, but a Client Component cannot import a Server Component (it can receive one as children).

Data. Fetch in Server Components with await. In Next.js 15 and later, params and searchParams are Promises that you await. Caching has changed between versions: older versions cached fetch by default, and Next.js 16 with Cache Components makes dynamic rendering the default and caching opt-in with the use cache directive. Check the docs of your version rather than relying on memory.

Mutations. A Server Action is an async function marked with "use server". Pass it to a form's action prop and it runs on the server with the FormData. After changing data, call revalidatePath or revalidateTag, or redirect. Treat actions as public endpoints: validate input and check authorization inside every action.

Navigation uses Link (with prefetching) and the useRouter hook in Client Components.

The example shows a Server Component page. It runs here as a plain async function, so it demonstrates the data flow, not Next.js itself.

jsx
// app/courses/[id]/page.jsx in a Next.js project: a Server Component (no "use client").
async function getCourse(id) {
  await new Promise((resolve) => setTimeout(resolve, 10));
  return { id, title: 'React Basics', lessons: ['JSX', 'Props', 'State'] };
}

export async function CoursePage({ params }) {
  const { id } = await params;
  const course = await getCourse(id);

  return (
    <article>
      <h1>{course.title}</h1>
      <ul>
        {course.lessons.map((lesson) => (
          <li key={lesson}>{lesson}</li>
        ))}
      </ul>
    </article>
  );
}

// Stand-in for the framework, which passes route params as a Promise.
export default async function Demo() {
  return CoursePage({ params: Promise.resolve({ id: 'react-basics' }) });
}

This is only a plain async function rendered to static HTML. It was not run inside Next.js, so routing, caching and streaming are not tested here. Rendered result: <article><h1>React Basics</h1><ul><li>JSX</li><li>Props</li><li>State</li></ul></article>

Key points

  • Folders are routes. page, layout, loading, error and not-found files define behaviour.
  • Components are Server Components by default. Add "use client" at a boundary for state, effects and browser APIs.
  • Server Components can be async and fetch directly. params is a Promise in current versions.
  • Server Actions handle mutations and must validate input and check authorization.
  • Caching differs between Next.js versions. Check the documentation for yours.

Common mistakes

  • Adding "use client" at the top of every file and losing the benefits of Server Components.
  • Importing a server-only module (database client) into a Client Component.
  • Forgetting to await params in current Next.js versions.
  • Trusting Server Action inputs without validation and authorization.
  • Assuming the caching rules of one Next.js version apply to another.

Follow-ups

  • What can you pass from a Server Component to a Client Component?
  • What does a layout do that a template does not?
  • How would you revalidate a list after a Server Action?

Tips

  • Say it is framework-specific. It shows you know where React ends.
  • Explain the client boundary rule with one example.
  • Mention that caching changed across versions and that you check the docs.