Compose a component tree
Build a tree from smaller components
Composition means forming a larger interface from smaller parts. A parent component describes where child components belong. The child can describe still more children, so a rendered page has a tree of components and host elements. Choose boundaries that clarify a meaningful responsibility, not one component for every single line of HTML.
function Materials() { return <p>Paper and pencils provided.</p>;}
function WorkshopCard() { return ( <article> <h2>Sketching basics</h2> <Materials /> </article> );}
export default function App() { return ( <main> <h1>Activities</h1> <WorkshopCard /> </main> );}Expected page: Activities, Sketching basics, and Paper and pencils provided. The component relationship is App → WorkshopCard → Materials. The DOM structure is different: main contains h1 and article; article contains h2 and p. App and Materials are not extra DOM wrappers. Use semantic HTML elements to express the document structure independently of JavaScript component boundaries.
Reuse a component in two places
function SafetyNote() { return <p>Keep the exit clear.</p>;}
export default function App() { return ( <main> <h1>Workshop rooms</h1> <section> <h2>Studio A</h2> <SafetyNote /> </section> <section> <h2>Studio B</h2> <SafetyNote /> </section> </main> );}Expected page: Workshop rooms, Studio A and Studio B, each with a paragraph saying Keep the exit clear. The single SafetyNote definition supplies two places in the tree. Editing the message changes both descriptions. Each occurrence has its own position; in later lessons React state can be associated with these positions. Reuse does not mean that all occurrences share local state.
Distinguish a component from an element
function Badge() { return <span>Beginner friendly</span>;}
export default function App() { const badgeElement = <Badge />; return <p>{badgeElement}</p>;}Badge is the function definition. The value in badgeElement is an element describing a Badge component. React processes it when it appears in the returned tree. The expected result is a paragraph containing a span with the text Beginner friendly. The element value itself is not a DOM node and does not immediately mount anything. Ordinary helper functions may be called to compute text, but use JSX for component boundaries.
Choose stable boundaries
Define reusable component functions at module scope rather than declaring them inside App. A new nested function is created on each invocation, and React can interpret it as a different component type; this matters for preserving state later. A large component can stay in one file until splitting it makes the code clearer. Avoid excessive wrappers, repeated IDs and unnecessary components that obscure reading order. Chapter 6 introduces props to vary the content of repeated components, and children-based composition for flexible wrappers.
Practice
Design a community noticeboard from a heading and two room notices. Draw its component tree and corresponding DOM tree, then explain which text will change if the reusable notice changes.