React: Greetings with props

Write one component and show it three times with different data.

What to build

A list of greetings made from one component.

Requirements

  • Write Greeting taking the props name and time, which renders a p with the class greeting and the text Good TIME, NAME!.
  • App renders, in a div, Greeting three times: for Ada in the morning, Alan in the afternoon and Grace in the evening.

Constraints

The text must be built from the props.

Example

The preview shows "Good morning, Ada!", "Good afternoon, Alan!" and "Good evening, Grace!".

Why it works

The component is written once and its props decide what it shows, so three uses give three different greetings.

Hints

Hint 1

function Greeting({ name, time }) returns a p.

Hint 2

Use the component three times in App with different props.

Solution

Show one correct solution (try it yourself first)
function Greeting({ name, time }) {
  return (
    <p className="greeting">
      Good {time}, {name}!
    </p>
  );
}

export default function App() {
  return (
    <div>
      <Greeting name="Ada" time="morning" />
      <Greeting name="Alan" time="afternoon" />
      <Greeting name="Grace" time="evening" />
    </div>
  );
}

Lesson: Props: passing data to components →