ReactEasy
Smart vs Dumb components?
"Smart" components handle logic: they fetch data, hold state and decide what happens. "Dumb" (presentational) components only render what they receive through props and report user actions through callbacks. Separating them makes UI pieces reusable and easy to test, though with hooks the split is now a guideline rather than a strict rule.
Smart (container) components
- Fetch and transform data, manage state, call APIs.
- Know about stores, routers and contexts.
- Pass data and callbacks down.
Dumb (presentational) components
- Render from props; little or no state (maybe UI state like "is open").
- No knowledge of where data comes from.
- Easy to reuse, test, and showcase in Storybook.
Modern perspective
- Custom hooks now hold most "smart" logic. A component can call
useProducts()and still be simple, so dedicated container components are less necessary. - Server Components (in frameworks) are natural "smart" components: they fetch data on the server and pass it to interactive Client Components.
- Dan Abramov, who popularized the pattern in 2015, later noted that he no longer suggests splitting components this way dogmatically.
When the split still helps
- Design-system components that must never fetch data.
- Testing complex UI states with plain props.
- Rendering the same UI with different data sources.
Code example
tsx
1// Presentational: pure UI, easy to test and reuse2type Product = { id: string; name: string; price: number };34export function ProductGrid({ products, onAdd }: { products: Product[]; onAdd: (id: string) => void }) {5 if (products.length === 0) return <p>No products found.</p>;6 return (7 <ul className="grid grid-cols-3 gap-4">8 {products.map((p) => (9 <li key={p.id}>10 {p.name} - ${p.price}11 <button onClick={() => onAdd(p.id)}>Add</button>12 </li>13 ))}14 </ul>15 );16}1718// Smart: data + behavior via hooks19import { useQuery, useMutation } from "@tanstack/react-query";2021export function ProductGridContainer({ category }: { category: string }) {22 const { data = [], isPending } = useQuery({23 queryKey: ["products", category],24 queryFn: () => fetch(`/api/products?category=${category}`).then((r) => r.json()),25 });26 const addToCart = useMutation({ mutationFn: (id: string) => fetch(`/api/cart/${id}`, { method: "POST" }) });2728 if (isPending) return <p>Loading…</p>;29 return <ProductGrid products={data} onAdd={(id) => addToCart.mutate(id)} />;30}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.