Reusable Components
Reusable components are components used more than once. One definition serves every place it appears.
Define once
Tip holds one short paragraph.
function Tip() {
return <p>Save your work often.</p>
}Use it twice
Two tags render two copies.
<>
<Tip />
<Tip />
</>Use it three times
A third tag adds a third copy.
<Tip />
<Tip />
<Tip />Example
function Tip() {
return <p>Save your work often.</p>
}
export default function App() {
return (
<>
<Tip />
<Tip />
<Tip />
</>
)
}
Key points
- A component definition can render many times.
- Each use is a separate tag in JSX.
- Repeated parts stay identical in every place.
- A fix in the definition changes every use at once.
In this example, Tip returns one paragraph. App returns Tip three times. Each tag renders the same paragraph. The page shows three identical lines.
Note
One definition can serve a whole page.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Why define a component once and use it many times?