Composing Components
Composing components means placing one component inside another component. A page grows from small parts joined together.
One component inside another
App returns Header as its only child.
function App() {
return <Header />
}Three parts in one page
The fragment holds Header, Main, and Footer.
<>
<Header />
<Main />
<Footer />
</>Order sets the layout
Header renders first. Footer renders last.
<Header />
<Footer />Example
function Header() {
return <h1>Welcome to my site</h1>
}
function Main() {
return <p>React builds pages from small pieces.</p>
}
function Footer() {
return <p>Thanks for visiting.</p>
}
export default function App() {
return (
<>
<Header />
<Main />
<Footer />
</>
)
}
Key points
- A component is a small piece of a page.
- App can return Header, Main, and Footer together.
- Each part keeps its own JSX.
- The parts render in the order they appear.
In this example, Header returns a heading. Main returns a short paragraph. Footer returns a closing line. App returns all three parts inside a fragment. The heading renders first. The closing line renders last.
Note
A fragment groups the parts without adding an extra element.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does composing components mean?