Components Can Call Other Components
What you'll learn: How to build complex UIs by nesting components inside each other, creating a component tree.
The Core Idea
Once you've created a function component, you can use it inside another component just like you'd use a built-in JSX element. This is called component composition, and it's the fundamental pattern for building React applications.
Instead of writing one massive component with all your UI logic, you break your interface into smaller, reusable pieces. Then you assemble those pieces by rendering them as JSX tags inside other components.
How It Works
When you want to use a component, you render it exactly like an HTML element:
function Greeting() {
return <p>Hello, friend!</p>;
}
function App() {
return (
<div>
<h1>My Application</h1>
<Greeting />
<Greeting />
</div>
);
}
Notice that App renders <Greeting /> twice. Each instance is independent—you're calling the Greeting function twice, getting separate pieces of JSX back.
The Component Tree Analogy
Think of your UI as a family tree. The App component is the parent. It has children: an h1 and two Greeting components. Each Greeting is also a parent—it has a p element as its child. This hierarchy is called the component tree.
You build from the bottom up: small, focused components that larger components assemble into a complete interface.
Why This Matters
Component composition lets you:
- Reuse UI patterns without copying code
- Organize complex interfaces into manageable pieces
- Reason about one small part at a time
Key Takeaway: Components become powerful when you nest them—render one component's JSX tag inside another to build a component tree, creating complex UIs from simple, reusable pieces.