Code Reference

Pages

React · Reference cheat sheet

Pages

React · Reference cheat sheet


📋 Overview

Pages are route-level screens: they compose features, fetch data, and sit inside layouts. In SPAs they map to router routes; in Next.js / Remix they are file-based routes.

🔧 Core concepts

  • Thin pages — orchestrate; push UI into feature components.
  • Data — loaders, React Query, or useEffect + fetch (prefer dedicated data libs).
  • ParamsuseParams, useSearchParams from the router.
  • Code splittinglazy(() => import("./Page")) + Suspense.

💡 Examples

import { useParams } from "react-router-dom";
import { TodoList } from "../features/todos/TodoList";

export function TodoPage() {
  const { listId } = useParams<{ listId: string }>();
  if (!listId) return <p>Missing list</p>;
  return (
    <section>
      <h1>List {listId}</h1>
      <TodoList listId={listId} />
    </section>
  );
}
import { lazy, Suspense } from "react";
import { Route, Routes } from "react-router-dom";

const TodoPage = lazy(() =>
  import("./pages/TodoPage").then((m) => ({ default: m.TodoPage })),
);

export function AppRoutes() {
  return (
    <Suspense fallback={<p>Loading…</p>}>
      <Routes>
        <Route path="/lists/:listId" element={<TodoPage />} />
      </Routes>
    </Suspense>
  );
}

⚠️ Pitfalls

  • Fat pages with all business logic — hard to test and reuse.
  • Fetching the same data in every page without a cache layer.
  • Forgetting loading and error UI for async pages.

On this page