Skip to content
tezvyn:

Container vs. Presentational: A Component Separation Pattern

Source: telerik.comMediumHow cards are made

Container vs. Presentational: A Component Separation Pattern

Split components into 'smart' containers that handle data and logic, and 'dumb' presentational components that just render UI. This lets you reuse state across different views, like showing a product list in a grid or a table.

Why it exists

Applications often need to display the same data in different ways. Without a clear separation, you might duplicate data-fetching and state management logic across multiple UI components. This leads to brittle code that's hard to maintain and test.

The mental model

Think of a restaurant. The 'container' is the chef in the kitchen who sources ingredients (fetches data), follows the recipe (business logic), and prepares the final dish. The 'presentational' component is the waiter who simply takes the finished plate from the kitchen and presents it to the customer (renders UI), without needing to know the recipe. The waiter can present the same dish on different types of plates, reusing the chef's work.

How it works

This pattern divides components into two distinct roles.

Container Components (Smart/Stateful): These components are the brains of the operation. They are responsible for fetching data from services or APIs, managing application state, and containing the business logic. They pass data down to their children via inputs and handle events emitted from them. They know how things work.

Presentational Components (Dumb/Stateless): These components are focused solely on the UI. Their job is to look good and display whatever data they are given via inputs. They are unaware of services, APIs, or where the data comes from. They communicate with their parent container only by emitting events. They know how things look.

When to use it

Use this pattern when building features that have complex state and logic, or when you anticipate needing to display the same data in multiple formats (e.g., a list and a grid). It is a foundational pattern for building scalable, reusable, and easily testable UIs in frameworks like Angular, React, and Vue.

When not to use it

For very simple, static components with no state or logic (like a footer), this separation is overkill. Applying the pattern too granularly can lead to excessive boilerplate code for simple features. Judge based on the component's complexity and potential for reuse.

One canonical example

A ProductPage container component uses a service to fetch a list of products. It then passes this list as an input to a ProductTable presentational component. The ProductTable simply renders the data in a table. If a user clicks a 'delete' button in a row, the ProductTable emits a delete event with the product's ID. The ProductPage container listens for this event and calls the service to perform the deletion.

Interview question

What is the primary advantage of separating components into Container and Presentational roles?

  • a.It eliminates the need for any form of inter-component communication.
  • b.It allows the same data and business logic to be applied to multiple user interface displays.Correct
  • c.It guarantees that all components in the application remain completely stateless.
  • d.It simplifies the component tree by reducing the overall number of components.
Why?

The pattern's core purpose is to allow the same data and business logic (managed by containers) to be presented in various UI forms (handled by presentational components), promoting reuse and maintainability. Option C is incorrect because container components are stateful, managing application state and logic.

Just read this? Test yourself on what you have been reading.

Read the original → telerik.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles