React
/Intermediate
Container vs Presentational
Definition
A classic React architecture pattern separating components into two categories: Containers (which handle data fetching and state) and Presentational components (which only render UI based on props).
Explain Like I'm New
Imagine a restaurant. The Container component is the Chef in the kitchen: gathering ingredients, cooking the food, and managing the chaos. The Presentational component is the Waiter: they don't know how to cook, they just take the finished plate (Props) and present it nicely to the customer on the table (UI).
Real World Example
A `UserProfileContainer.js` component executes `useEffect` to fetch user data, and then returns `<UserProfile data={userData} />`. The `UserProfile.js` is a pure function that just renders HTML.
Common Use Cases
- •Separating concerns (Logic vs UI)
- •Making UI components highly reusable across different data sources
- •Making UI components easily testable with Snapshot testing
Interactive Example
Loading Code Playground...
Interview Questions
basic
- What is a Presentational Component?
- What is a Container Component?
intermediate
- Why is this pattern less common today than it was in 2018?
- Can Presentational components have state?
advanced
- How did React Hooks largely replace the need for Container components?
- When is it still a good idea to use this pattern?