Part 02 / Topic 6 of 7
When and how to split a UI into components
There is no single rule for when a component has gotten "too big," but these are the four signals worth checking before you decide:
| Signal | What it looks like |
|---|---|
| Repetition | You are about to copy-paste a chunk of JSX to use it a second time - Part 1's Panel/WalletSummary and this part's ProductCard both exist because of this signal alone |
| A named concept | A chunk of JSX has a name you could say out loud - "the header," "the items list," "the total" - even if you never plan to reuse it elsewhere |
| Independent props | A chunk of the JSX only ever needs a handful of the parent's props, not all of them - that chunk is a candidate for its own component with its own, smaller prop list |
| Independent testing | You want to assert something about one piece ("the total shows ₹540") without rendering or reasoning about the whole screen at once |
Splitting is a structural change, not a behavioral one - it should never change what ends up on screen. examples/04_breaking_up_ui.jsx renders the exact same Meera General Store receipt two ways: one giant block, and one composed from three named pieces using the second and third signals above (each piece has a name, and a smaller prop list than the whole receipt):
// examples/04_breaking_up_ui.jsx (composed version, trimmed)
export function OrderSummary({ customerName, items, total }) {
return (
<section className="order-summary">
<OrderHeader customerName={customerName} />
<OrderItemsList items={items} />
<OrderTotal total={total} />
</section>
);
}
examples/04_breaking_up_ui.test.jsx renders both OrderSummaryMonolith and OrderSummary with the same props through react-dom/server's renderToStaticMarkup (the same tool Part 1's section 5 used to prove JSX equals createElement) and really confirms the two strings are identical, character for character:
composed === monolith: true
That equality is the whole point: decomposing a component is a refactor for the person reading the code, not a change for the person looking at the browser.
Done here? The practice set for Part 2 — including the solutions — is on the part page.