React: A layout with nested components

Components can contain components that contain components. Build a three-level tree.

What to build

A layout with a sidebar menu and an article, built from nested components.

Requirements

  • Write the components Sidebar (an aside with an h2 Menu and a ul of three li: Home, Lessons, Practice), Article (an article with an h2 Welcome and a p Pick a lesson to start.) and Layout (a div with the class layout that renders Sidebar then Article).
  • App renders a Layout and nothing else.

Constraints

App's JSX must contain only the Layout component.

Example

The preview shows the menu with three items and, after it, the Welcome article.

Why it works

Layout groups the two parts so that App stays one line, and each level of the tree only knows about the level directly below it.

Hints

Hint 1

A component can use other components: Layout can render <Sidebar /> and <Article />.

Hint 2

Give the outer div the class layout, so the CSS-free check can find it.

Solution

Show one correct solution (try it yourself first)
function Sidebar() {
  return (
    <aside>
      <h2>Menu</h2>
      <ul>
        <li>Home</li>
        <li>Lessons</li>
        <li>Practice</li>
      </ul>
    </aside>
  );
}

function Article() {
  return (
    <article>
      <h2>Welcome</h2>
      <p>Pick a lesson to start.</p>
    </article>
  );
}

function Layout() {
  return (
    <div className="layout">
      <Sidebar />
      <Article />
    </div>
  );
}

export default function App() {
  return <Layout />;
}

Lesson: What React is: components and rendering →