Skip to content
Tutorials Center
My learning

Topics

Lesson 68 of 132 ยท Advanced Patterns

Container and Presentational Components

Learn the container and presentational component pattern - what each part does, why it makes UI easy to reuse and test, how custom hooks replaced class containers, and when not to split.

Open an older React codebase and you will often find components in pairs: UserListContainer and UserList. The first fetches data and holds state, the second only draws. This is the container and presentational pattern (also called smart and dumb components), popularized around 2015 by Dan Abramov. It was an answer to a real problem: components that mix data logic and markup are hard to reuse, hard to test and hard to restyle.

Hooks changed how you split, but not why. This lesson explains the pattern, shows the same screen written three ways (mixed, split by components, split by a custom hook), and gives honest rules for when the split pays off and when it is over-engineering. It builds on props, lifting state up and custom hooks.

The two roles

Presentational components answer "how does it look?"

  • They receive data and callbacks through props and render markup.
  • They have little or no state of their own (maybe a UI detail such as "is the menu open").
  • They do not know where data comes from: no fetching, no stores, no routing.
  • They are easy to reuse, easy to test (props in, markup out) and easy to show in tools such as Storybook.

Container components answer "how does it work?"

  • They load and transform data, own the state, and define what the callbacks do.
  • They render presentational components and pass them props.
  • They contain little markup or styling of their own.
flowchart TD
  C["Container: fetches data, owns state, defines handlers"] -->|"props: data + callbacks"| P["Presentational: renders markup, calls callbacks"]
  P -->|"calls onAdd, onToggle..."| C

Data flows down as props, events flow up as callbacks.

Starting point: one component that does everything

Here is a small todo screen where data, handlers and markup are tangled together. It works, but you cannot show it with different data in a style guide, you cannot test the markup without the state, and the markup cannot be reused for a different data source.

Before: logic and markup mixed

Split by components

Now separate the roles. TodoList, TodoForm and Summary are presentational: they take props and call callbacks. TodoContainer owns the state and passes things down.

After: presentational components and a container

The visible behavior is the same, but now TodoList renders from any array: here from state, below from a constant. That reuse and that testability (render TodoList with a fixed array and assert on the markup) are the benefits of the pattern.

How hooks changed it

In the class era a container had to be a class (it needed state and lifecycle methods) wrapped around a function. The cost was an extra component for every screen. With hooks, a custom hook can hold the logic and the same component can use it, so you split the logic from the markup without adding a component:

JSX
// useTodos.js: the "container" logic is now a hook
function useTodos(initial) {
  const [todos, setTodos] = useState(initial);
  const add = (text) => setTodos((list) => [...list, { id: Date.now(), text, done: false }]);
  const toggle = (id) => setTodos((list) => list.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
  const remaining = todos.filter((t) => !t.done).length;
  return { todos, add, toggle, remaining };
}
Logic in a hook, markup in the component

There is no separate container component, yet the roles are intact: useTodos is the "how it works", TodoList is the "how it looks", and App only wires them. You can test useTodos without rendering anything (see testing hooks and logic).

Component-based architecture

The pattern is one instance of a broader idea: build the UI as a tree of small components with clear responsibilities. A useful way to think about layers:

Layer Knows about Example
UI primitives Only props and styling Button, Card, Modal
Presentational components Props and primitives TodoList, UserCard
Containers / hooks Data sources, state, side effects useTodos, useUser, a page component
Pages / routes URLs, layouts, which containers to show /todos page

Information flows down as props and up as callbacks, and each layer depends only on the layers below it. The app architecture lesson shows how to organize folders around this.

When not to split

The pattern is a tool, not a rule. Splitting costs something: more files, more props to thread through, and indirection when you read the code.

  • Do not split a tiny component that is used once and has five lines of logic.
  • Do not create containers that only pass props along (they add a layer but no value). If a container's only job is return <List {...props} />, delete it.
  • Do split when the markup is reused with different data, when you want it in a style guide or in tests without the data layer, when the logic is complex enough to deserve its own tests, or when designers and developers work on the pieces separately.
  • Prefer a hook over a container component in modern code, unless the container must make a decision about what to render (for example showing a loading state), where a component is the natural unit. Server Components bring the same split to the server: a Server Component loads data and renders Client Components for the interactive parts.

Common mistakes

  • Presentational components that fetch data "just this once". They become impossible to reuse and test.
  • Props explosion: passing twenty individual props. Group related ones into an object or use composition (children and composition).
  • Naming by suffix alone. Container in the name does not make a good design; name components by what they are (TodoPage, TodoList).
  • Duplicating state in the container and the presentational component. Keep one owner.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which statement describes a presentational component?

    Presentational components focus on how things look, which makes them easy to reuse, test and show in isolation.
  2. 2. Why was the container usually a class before hooks?

    Before hooks, only classes could hold state and run lifecycle code, so containers were class components wrapping function components.
  3. 3. How do custom hooks change the pattern?

    A hook such as useTodos holds the container logic. The component stays a thin wiring layer, and the logic can be tested alone.
  4. 4. Which is a good reason not to split a component?

    A container that only passes props adds indirection without benefit. Remove it or give it a real responsibility.
  5. 5. In the layered view, which direction does data normally flow?

    Parents pass data down as props, and children report events up through callback props.

Summary

  • Presentational components render from props; containers own data, state and behavior.
  • Splitting makes UI reusable, testable and easy to show in isolation.
  • Custom hooks replaced class containers: put logic in a hook, keep the component thin.
  • Data flows down as props and events flow up as callbacks.
  • Do not split for its own sake; remove containers that only pass props through.

Next: Compound Components