Split a page into components, each responsible for one part.
What to build
A page made of a header, a content area and a footer, each its own component.
Requirements
- Write components
Header(aheaderelement with anh1sayingMy Blog),Content(amainelement with anh2sayingFirst postand apsayingHello from React.) andFooter(afooterelement with apsayingWritten with React). Apprenders them in the order Header, Content, Footer inside onediv.
Constraints
Each part must be its own component.
Example
The preview shows "My Blog", then the post's title and text, then "Written with React" at the bottom.
Why it works
Each component owns one part of the page, so a change to the footer cannot break the header, and App reads like an outline of the page.
Hints
Hint 1
Write function Header(), function Content() and function Footer(), each returning one element.
Hint 2
App returns a div that uses the three components in order.
Solution
Show one correct solution (try it yourself first)
function Header() {
return (
<header>
<h1>My Blog</h1>
</header>
);
}
function Content() {
return (
<main>
<h2>First post</h2>
<p>Hello from React.</p>
</main>
);
}
function Footer() {
return (
<footer>
<p>Written with React</p>
</footer>
);
}
export default function App() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
}