📖 Scenario: You are building a simple React app that shows a greeting message for different users. Each greeting should show the user's name inside a friendly message.
🎯 Goal: Build a React functional component called Greeting that uses props to display a personalized message with the user's name.
📋 What You'll Learn
Create a functional component named Greeting that accepts props.
Use props.name inside JSX to show the user's name.
Render the Greeting component with the name "Alice".
Render the Greeting component with the name "Bob".
💡 Why This Matters
🌍 Real World
Using props in JSX is how React apps show dynamic data like user names, messages, or product info.
💼 Career
Understanding props is essential for React developers to build reusable and flexible UI components.
Progress0 / 4 steps
1
Create the Greeting component
Create a React functional component called Greeting that accepts props as a parameter.
React
Hint
Start by writing a function named Greeting that takes props as input.
2
Use props.name inside JSX
Inside the Greeting component, return a <div> element that contains the text Hello, followed by the value of props.name.
React
Hint
Use JSX syntax to embed props.name inside curly braces within the <div>.
3
Render Greeting with name 'Alice'
Render the Greeting component with the prop name set to "Alice" inside a root <div>.
React
Hint
Use JSX to render <Greeting name="Alice" /> inside a <div>.
4
Render Greeting with name 'Bob'
Add another Greeting component inside the same root <div> with the prop name set to "Bob" below the first one.
React
Hint
Render <Greeting name="Bob" /> below the first greeting inside the same <div>.
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 internal state within a component
C. To style components with CSS
D. To pass data from a parent component to a child component
Solution
Step 1: Understand the role of props
Props are used to send data from a parent component to a child component in React.
Step 2: Differentiate props from other concepts
State stores internal data, events handle user actions, and CSS styles components, but props specifically pass data down.
Final Answer:
To pass data from a parent component to a child component -> Option D
Quick Check:
Props = Data passing [OK]
Hint: Props always send data 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 use a prop named title inside JSX?
easy
A. <h1>title</h1>
B. <h1>{title}</h1>
C. <h1>{props.title}</h1>
D. <h1>$title</h1>
Solution
Step 1: Use curly braces to embed prop values in JSX
In JSX, to show a prop value, you wrap the prop access in curly braces like {props.title}.
Step 2: Understand context of prop usage
The props object is passed to the component function, so access via {props.title}. Using {title} requires destructuring like function Comp({title}).
Final Answer:
<h1>{props.title}</h1> -> Option C
Quick Check:
Curly braces show prop values [OK]
Hint: Use curly braces { } to show props in JSX [OK]
Common Mistakes:
Writing prop name as plain text without braces
Using $ sign like in other languages
Using {title} without destructuring the props object