What is the Container/Presentational pattern? Is it still relevant?
Hint
Smart vs dumb components — data fetching separated from rendering; largely replaced by hooks but the principle remains
Full Answer
The pattern separates data concerns (fetching, state) from UI concerns (rendering, styling):
// ❌ Old way — explicit Container component
function UserListContainer() {
const [users, setUsers] = useState([]);
useEffect(() => { fetchUsers().then(setUsers); }, []);
return <UserList users={users} />; // pass data down
}
function UserList({ users }) {
return users.map(u => <UserCard key={u.id} user={u} />);
}
// ✅ Modern way — custom hook achieves the same separation
function useUsers() {
return useQuery({ queryKey: ['users'], queryFn: fetchUsers });
}
// Presentational component stays the same
function UserList({ users }) {
return users.map(u => <UserCard key={u.id} user={u} />);
}
// Consuming component — much simpler
function UsersPage() {
const { data: users } = useUsers();
return <UserList users={users ?? []} />;
}
Still relevant principles:
- Keep components that render UI free of data-fetching — easier to test, reuse in Storybook
- The separation now lives in custom hooks, not wrapper components
💡 The pattern's goal (separation of concerns) is still correct; the implementation changed. Hooks achieve it more elegantly without extra component nesting.
More Component Patterns Questions
Practice this in a timed sprint
5 free questions, no signup required