React: A page from three components

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 (a header element with an h1 saying My Blog), Content (a main element with an h2 saying First post and a p saying Hello from React.) and Footer (a footer element with a p saying Written with React).
  • App renders them in the order Header, Content, Footer inside one div.

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>
  );
}

Lesson: What React is: components and rendering →