React Container-Presenter Pattern Explained

⚛️ Top 150 React Interview Questions – 109/150 📌 Topic: Container-Presenter Relevance ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHAT is it? The Container-Presenter pattern splits a component into two parts: 🟢 Container (Smart Component) Handles logic, state, API calls, and data processing 🔵 Presenter (Dumb Component) Handles UI and styling only Receives data via props It separates how it works from how it looks. ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHY use this pattern? 🧩 Separation of Concerns Logic and UI live in different files ♻️ Reusability Same Presenter can work with different Containers 🧪 Better Testing Test UI and business logic independently 🧹 Cleaner Codebase Prevents huge messy components ━━━━━━━━━━━━━━━━━━━━━━ 🔹 HOW do you implement it? 1️⃣ Presenter (UI Only) const UserList = ({ users }) => ( <ul> {users.map(u => ( <li key={u.id}>{u.name}</li> ))} </ul> ); 2️⃣ Container (Logic Only) const UserContainer = () => { const [users, setUsers] = useState([]); useEffect(() => { // Fetch logic here }, []); return <UserList users={users} />; }; 👉 Container manages data 👉 Presenter renders UI ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHERE is it relevant today? 🏗️ Legacy React/Redux Projects Very common in older architectures 📊 Large Applications When logic becomes too complex to stay inside one component ⚠️ Modern Note Hooks often replace this pattern today. You can extract logic into custom hooks without needing a separate Container file. ━━━━━━━━━━━━━━━━━━━━━━ 📝 SUMMARY (Easy to Remember) Think of a restaurant 🍽️ 👨🍳 Kitchen (Container) → Handles ingredients & cooking (logic/data) 🧑🍽️ Waiter (Presenter) → Serves the food (UI) The waiter doesn’t know how the food was cooked. The kitchen doesn’t care how it’s presented. Clear roles. Clean system. ━━━━━━━━━━━━━━━━━━━━━━ 👇 Comment “React” if this handbook is helping you 🔁 Share with someone preparing for React interviews #ReactJS #ReactInterview #DesignPatterns #ContainerPresenter #FrontendArchitecture #Top150ReactQuestions #LearningInPublic #Developers ━━━━━━━━━━━━━━━━━━━━━━

  • text, letter

To view or add a comment, sign in

Explore content categories