📖 Scenario: You are building a simple React app that shows a greeting message for a user. The greeting text should come from a parent component and be passed down to a child component.
🎯 Goal: Build two React functional components: App and Greeting. Pass a message prop from App to Greeting and display it inside Greeting.
📋 What You'll Learn
Create a functional component called App.
Inside App, create a variable greetingMessage with the exact string 'Hello, welcome to React!'.
Create a functional component called Greeting that accepts a message prop.
Render the Greeting component inside App and pass greetingMessage as the message prop.
Inside Greeting, display the message prop inside a <h1> element.
💡 Why This Matters
🌍 Real World
Passing props is how React components share data, like sending user info or settings from a main app to smaller parts of the UI.
💼 Career
Understanding props is essential for React developers to build reusable and interactive components that work together.
Progress0 / 4 steps
1
Create the App component with a greeting message
Create a React functional component called App. Inside it, create a variable called greetingMessage and set it to the string 'Hello, welcome to React!'.
React
Hint
Remember to use const to create the greetingMessage variable inside the App function.
2
Create the Greeting component that accepts a message prop
Create a React functional component called Greeting that accepts a single prop named message. The component should return a <div> element for now.
React
Hint
Use destructuring in the function parameter to get the message prop.
3
Pass the greetingMessage prop from App to Greeting
Inside the App component, return the Greeting component and pass the variable greetingMessage as the message prop.
React
Hint
Use JSX syntax to render <Greeting message={greetingMessage} /> inside App.
4
Display the message prop inside Greeting
Inside the Greeting component, replace the <div> content with an <h1> element that displays the message prop.
React
Hint
Use curly braces {} inside JSX to insert the message prop value.
Practice
(1/5)
1. What is the main purpose of props in React components?
easy
A. To handle user events like clicks
B. To store local state inside a component
C. To pass data from a parent component to a child component
D. To style components with CSS
Solution
Step 1: Understand the role of props
Props are used to send data from one component to another, specifically from parent to child.
Step 2: Differentiate props from other React features
State stores local data, events handle interactions, and CSS styles components, but props specifically pass data down.
Final Answer:
To pass data from a parent component to a child component -> Option C
Quick Check:
Props = data passing [OK]
Hint: Props always send data down from parent to child [OK]
Common Mistakes:
Confusing props with state
Thinking props handle events
Believing props style components
2. Which of the following is the correct way to pass a prop named title with value "Hello" to a component Header?
easy
A.
B.
C.
D.
Solution
Step 1: Recall JSX prop syntax
Props are passed like HTML attributes: name="value" or name={expression}.
Step 2: Check each option
uses correct string syntax. misses quotes around string. uses invalid quotes. uses colon which is invalid in JSX.