Design Patterns

React Design Patterns: A Practical Reference

· 9 min read

React Design Patterns: A Practical Reference
Contents

When the Gang of Four published Design Patterns in 1994, OOP developers got a shared vocabulary for recurring structural problems. SOLID later added principles to judge design decisions by. React doesn’t fit neatly into either, but after years of wide use it has grown idiomatic patterns of its own.

They are named answers to problems that keep coming back in React code: how to share logic without copying it, how to avoid prop drilling, how to split server and client concerns, how to give a component a flexible API. Below are the six I’d call the most important, with an everyday analogy, a code sample and a diagram for every one.

Contents#

  1. Composition Pattern
  2. Custom Hook Pattern
  3. Provider Pattern (Context / Dependency Injection)
  4. Compound Components Pattern
  5. Server ↔ Client Component Separation Pattern
  6. Suspense Pattern

1. Composition Pattern#

Composition means building a complex UI out of smaller, reusable components. React doesn’t want you to inherit behavior from parent classes. You assemble the interface from independent pieces instead.

The rule is older than React. “Favor object composition over class inheritance” comes from that same Gang of Four book. The old React docs put it even more bluntly: Facebook used React in thousands of components and found no case where they’d recommend component inheritance hierarchies (Composition vs Inheritance).

Think of a house. You don’t make a new wall by inheriting from an existing one. You put the house together from rooms, doors, windows and furniture, and each piece has its own job. React components work the same way.

A dashboard is the usual example. Header, sidebar, statistics cards, charts and footer are built separately and composed into the page:

function Dashboard() {
  return (
    <Page>
      <Header />
      <Sidebar />
      <Content />
      <Footer />
    </Page>
  );
}

Dashboard doesn’t do much on its own. It arranges smaller components, and each of those stays small, can be reused on another page and tested separately. In my opinion this is the most important pattern of the six, simply because React’s whole component model is built around it. Most designs that would rely on inheritance in classic OOP end up as composition in React.

Composition pattern diagram
Composition in React illustrated as independent UI elements assembled into a dashboard.

2. Custom Hook Pattern#

A custom hook pulls reusable stateful logic out of a component into a plain function. It can hold state, side effects, data fetching, event handling and business rules without being tied to any particular UI.

Before hooks arrived in React 16.8 (early 2019), we shared behavior through Higher-Order Components (HOCs, functions that take a component and return a wrapped one) and render props. Both work, but each one wraps your component in another component. Stack a few and you get a “wrapper hell” in React DevTools, plus props that show up from nowhere. Hooks made this a lot simpler, since logic is reused through an ordinary function call.

Take a coffee machine in an office. Everybody uses it and nobody knows how it heats water, grinds beans or keeps the pressure. You press a button and get coffee. A component uses a custom hook the same way, without caring how it works inside.

function useCurrentUser() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    fetch("/api/user")
      .then(res => res.json())
      .then(setUser);
  }, []);

  return user;
}

function Profile() {
  const user = useCurrentUser();

  return <h1>{ user?.name }</h1>;
}

Profile only renders. Where the user comes from is the hook’s business, so any other component can call useCurrentUser() and get the same logic. Authentication, API requests, form handling, theme management and browser event listeners are typical candidates for a hook.

Keep in mind this hook is a demo. It has no error handling and no way to cancel the request. Under Strict Mode in development, React 18 also runs the effect twice, so you’ll see two requests in the Network tab. For real data fetching the React docs themselves point you to a framework’s built-in fetching or a client-side cache like TanStack Query or SWR rather than a raw effect.

Custom Hook pattern diagram
Custom Hook pattern in React, showing reusable logic extracted from component rendering.

3. Provider Pattern (Context / Dependency Injection)#

Say an avatar five levels down the tree needs the current user. Without a provider, every component in between takes a user prop only to pass it on. That’s prop drilling. The Provider pattern shares data and services across a component tree so nobody has to pass props through every level.

React does it with Context. A provider works a lot like a Dependency Injection container: it makes dependencies available to everything below it.

Picture a building with a central power system. Every room has electricity without its own generator, and no room knows where the power comes from.

const AuthContext = createContext();

function AuthProvider({ children }) {
  const [user] = useState({
    name: "John"
  });

  return (
    <AuthContext.Provider value={ user }>
      { children }
    </AuthContext.Provider>
  );
}

function Profile() {
  const user = useContext(AuthContext);

  return <h1>{ user.name }</h1>;
}

Profile asks for AuthContext and gets the value from the nearest AuthProvider above it. Nothing in between knows about the user. Authentication, theme, localization, feature flags, analytics and shared state are the usual things to put behind a provider. They are cross-cutting concerns (needed all over the app, not by one feature), and that’s where the pattern fits best.

But there is a catch. When the provider’s value changes, every component that reads the context re-renders. Pass an object literal like value={ { user, setUser } } and it changes on every render of the provider. For the logged-in user or the theme this hardly matters, they rarely change. For something that updates on every keystroke it does. So I’d keep Context for app-wide values that change rarely, and for busy shared state split the context or use a store with selectors.

Provider pattern illustration
Provider pattern in React, showing shared context delivered through the component tree.

4. Compound Components Pattern#

Think of a restaurant (it will come up twice more below). Reception, dining area, kitchen and waiters each do a different job under one shared system. Separately they’re not worth much. Together you get a dinner out.

Compound components work like that. Several components act as one coordinated unit, the parent keeps the shared state and behavior, and each child handles one specialized part. Many component libraries build their primitives this way: you assemble <Tabs.List />, <Tabs.Trigger /> and <Tabs.Content />, and the implementation coordinates them behind the scenes. Radix UI Tabs has exactly these parts.

<Tabs>
  <Tabs.List>
    <Tabs.Trigger value="profile">
      Profile
    </Tabs.Trigger>

    <Tabs.Trigger value="settings">
      Settings
    </Tabs.Trigger>
  </Tabs.List>

  <Tabs.Content value="profile">
    Profile Content
  </Tabs.Content>

  <Tabs.Content value="settings">
    Settings Content
  </Tabs.Content>
</Tabs>

So how does Tabs.Trigger know which tab is active? Through context. Inside, Tabs is a provider holding the active value, and Tabs.Trigger and Tabs.Content read it. It’s pattern 3 again with a nicer API on top. The consumer gets markup that reads almost like a description of the UI, and all the coordination stays hidden. That flexibility is why design systems use it so much.

Compound Components pattern diagram
Compound Components pattern in React, showing coordinated child components within a parent container.

5. Server ↔ Client Component Separation Pattern#

Server Components and Client Components have different jobs. Server Components run on the server and take care of data fetching, database access, authentication and caching. Client Components run in the browser and handle state, user interactions, event handlers and browser APIs. The pattern keeps business logic and interactivity clearly apart.

Back to the restaurant: the kitchen cooks and the dining area serves. The kitchen doesn’t take orders, the dining area doesn’t cook.

Next.js App Router works this way by default. Pages and layouts are Server Components unless you mark them as client.

app/products/page.jsx (Server Component):

export default async function ProductsPage() {
  const products = await getProducts();

  return (
    <ProductsList products={ products } />
  );
}

ProductFilter.jsx (Client Component):

"use client";

export function ProductFilter() {
  const [search, setSearch] = useState("");

  return (
    <input
      value={ search }
      onChange={ e => setSearch(e.target.value) }
    />
  );
}

ProductsPage is an async function that awaits data right in the render, which only a Server Component can do. Its code never ships to the browser, so there’s less JavaScript to download and hydrate. ProductFilter needs useState and an onChange handler, so it gets "use client".

What about the boundary itself? "use client" applies to the file and to everything it imports. Put it on a page and the whole page goes to the browser as JavaScript. From my experience the directive belongs as deep in the tree as possible, on the small interactive leaf like ProductFilter, never on the page or layout around it.

Server ↔ Client pattern diagram
Server ↔ Client separation in React: server handles data fetching and business logic, client manages state and user interactions.

6. Suspense Pattern#

And the restaurant one last time. A good one doesn’t make you wait until every dish is ready. Bread and drinks come right away and the rest arrives when it’s done.

Suspense does the same with rendering. React pauses a part of the tree while an async operation finishes and shows fallback content meanwhile. The rest of the page doesn’t wait. Content appears as it becomes available, and that’s what makes streaming possible.

<Suspense fallback={ <Spinner /> }>
  <Products />
</Suspense>

While Products loads, users see <Spinner /> instead of a blank page. The data takes as long as before, but the page feels faster. Typical uses are data fetching, lazy-loaded components, streaming Server Components and progressive rendering in general.

But will this boundary show the spinner for useCurrentUser from pattern 2? It won’t. Suspense only reacts to code that suspends: a component loaded with lazy, an async Server Component, a promise read with use, or a data library with Suspense support. A fetch inside useEffect renders null first and updates later, and Suspense never notices (the Suspense docs say so explicitly).

Suspense pattern diagram
Suspense pattern in React: displaying fallback content while asynchronous operations complete, enabling progressive rendering.

Putting it together#

These six rarely show up alone. A typical Next.js page uses most of them at once. Server/client separation decides where the data is fetched, and Suspense covers the wait while it arrives. A provider hands down auth or theme without prop drilling. The layout is composed from focused pieces, custom hooks hold the interactive logic, and compound components give complex UI elements a clean API.

Knowing the patterns by name also speeds up architecture decisions and makes code reviews more precise. When a reviewer writes “extract that into a custom hook” or “use compound components here”, nobody has to guess what’s meant.

React has more patterns than these. From my experience these six are the ones you meet most often, and the ones I’d reach for first.