Sub topic 9
Composing components into larger interfaces
Composition means building a larger interface by nesting smaller components inside one another. This chapter assumes you can write function components, use JSX with curly braces, and pass props and children. It uses modern JavaScript: destructuring, arrow functions, and ES modules.
The working model
A React component is a function that returns JSX. JSX can contain other components, so a component can be built from other components. The result is a tree. The root component renders its children, and those render their own children, until only built-in elements such as div and p remain.
Data flows down this tree through props. A parent decides what each child receives. A child never reaches up and changes the parent's variables.
Composing with props
The first way to compose is to render a component inside another and pass it the values it needs.
function Avatar({ src, name }) {
return <img className="avatar" src={src} alt={name} />;
}
function Bio({ name, text }) {
return (
<div>
<h3>{name}</h3>
<p>{text}</p>
</div>
);
}
function ProfileSummary({ user }) {
return (
<div className="summary">
<Avatar src={user.imageUrl} name={user.name} />
<Bio name={user.name} text={user.bio} />
</div>
);
}ProfileSummary holds no layout details of its own for the image or text. It only decides which pieces appear and which data each one gets. Each piece can be changed or reused without touching the others.
Composing with children
The second way is to let the parent supply the content. A generic wrapper renders children and adds its own structure around them.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}
function ProfilePage({ user }) {
return (
<Card title="Profile">
<ProfileSummary user={user} />
</Card>
);
}Card knows nothing about profiles. It supplies a frame and a title, and any content fits inside. This is the usual way to build layouts, panels, and modals.
Choosing between the two
| Situation | Prefer |
|---|---|
| The child needs specific data from the parent | Props on a named component |
| The wrapper should work with any content | `children` |
| A component needs two or more separate content areas | Extra props that take JSX |
For the last case, a prop can hold JSX, as in <Layout sidebar={<Nav />} content={<Feed />} />. Inside Layout, you render {sidebar} and {content} where you want them.
Common errors
- Defining a component inside another component. The inner function is a new component on every render, so React discards and rebuilds its subtree each time, and any state inside is lost. Define components at the top level of the module.
- Lowercase component names.
<card />is treated as an HTML tag. Component names must start with a capital letter. - Passing everything through many layers. Forwarding the same props through several levels makes code hard to follow. Often the fix is to pass finished JSX as
childreninstead of the raw data. - Splitting too early. Extract a component when a piece is reused, has a clear name, or makes the parent hard to read. Tiny wrappers with no purpose add indirection and no benefit.
Exercise
Exercise
You write function Page() { function Panel({ children }) { return <div>{children}</div>; } return <Panel>Hi</Panel>; }. Why is defining Panel inside Page a problem, and how do you fix it?
Show answer
Each time Page renders, Panel becomes a new function, so React treats it as a different component. It unmounts and remounts the subtree, which loses state and wastes work. Move Panel out to the top level of the module and render it from Page.
Exercise
A Layout component must show a navigation area and a main area in different places. Which composition technique fits, and what does a call look like?
Show answer
Use props that accept JSX, because there are two separate content areas. For example, <Layout nav={<Nav />} main={<Feed />} />, with Layout rendering {nav} and {main} in their places.