Nesting Components: The Basic Pattern
What you'll learn: How to render one component inside another to create hierarchical UI structures.
The Core Idea
Once you've created multiple function components, you can compose them by rendering one inside another. This is called "nesting" — just like HTML elements nest (<div><p>Hello</p></div>), React components nest the same way.
When you return JSX from a component, you can include other components as if they were HTML tags. React will execute those nested components and insert their output into the parent's output.
How It Works
function Greeting() {
return <h1>Welcome!</h1>;
}
function Header() {
return (
<header>
<Greeting />
<p>Please sign in</p>
</header>
);
}
Here, Header nests Greeting inside its JSX. When React renders Header, it sees <Greeting />, executes the Greeting function, gets <h1>Welcome!</h1> back, and inserts it into the final output.
The result is equivalent to:
<header>
<h1>Welcome!</h1>
<p>Please sign in</p>
</header>
Think of It Like Building Blocks
Imagine components as LEGO bricks. A Header brick contains a Greeting brick inside it. You snap them together, and React builds the final structure automatically.
Multiple Nestings
function App() {
return (
<div>
<Header />
<Header />
</div>
);
}
You can nest the same component multiple times. Each <Header /> executes independently.
Key Takeaway: Nesting components means rendering one component's JSX tag inside another component's return statement, creating a parent-child hierarchy that builds complex UIs from simple, reusable pieces.