This pattern splits components into container components, which handle how things work (fetching data, holding state, connecting to stores, defining callbacks), and presentational components, which handle how things look (they receive data and callbacks via props, render markup and styles, and hold no business logic). The benefit is separation of concerns: presentational pieces are reusable, easy to test, and easy to put in Storybook. Since hooks, the strict split is no longer necessary. Custom hooks extract the logic instead of wrapper components. The underlying idea, keeping UI components dumb and logic in a separate unit, still matters, and Server vs Client Components echo it.
// Presentational: pure UI, props in, events out
function UserList({ users, onSelect, selectedId }) {
return (
<ul role="listbox" aria-label="Users">
{users.map(u => (
<li
key={u.id}
role="option"
aria-selected={u.id === selectedId}
onClick={() => onSelect(u.id)}
>
{u.name}
</li>
))}
</ul>
);
}
// Container: data and behaviour
function UserListContainer() {
const { data: users = [] } = useQuery({ queryKey: ['users'], queryFn: fetchUsers });
const [selectedId, setSelectedId] = useState(null);
return <UserList users={users} selectedId={selectedId} onSelect={setSelectedId} />;
}
UserList works with data from anywhere.function useUsers() {
const query = useQuery({ queryKey: ['users'], queryFn: fetchUsers });
const [selectedId, setSelectedId] = useState(null);
return { users: query.data ?? [], selectedId, setSelectedId };
}
Spotted a mistake or a rights concern? Report an issue