📖 Scenario: You want to create a simple React component that shows a greeting message. The message should come from outside the component so you can reuse it with different greetings.
🎯 Goal: Build a React component that receives a greeting message as a prop and displays it inside a paragraph.
📋 What You'll Learn
Create a functional React component named Greeting
Use a prop named message to pass the greeting text
Render the message inside a <p> element
Use the Greeting component inside App and pass the message 'Hello, friend!'
💡 Why This Matters
🌍 Real World
Props let you build flexible UI parts that change based on data, like showing user names or messages.
💼 Career
Understanding props is essential for React developers to create reusable and maintainable 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
Add a message prop
Inside the Greeting component, use props.message to access the greeting text.
React
Hint
Use curly braces {} inside JSX to show the value of props.message.
3
Use Greeting in App
Create a React functional component called App that returns the Greeting component with the message prop set to 'Hello, friend!'.
React
Hint
Return the Greeting component with the message prop inside App.
4
Export App component
Add export default App; at the end of the code to make App the main component.
React
Hint
This line makes App the component React shows on the page.
Practice
(1/5)
1. What are props in React?
easy
A. Data passed from a parent component to a child component
B. A way to store component state internally
C. Functions that update the component's state
D. A method to style components
Solution
Step 1: Understand the role of props
Props are used to send data from a parent component down to a child component in React.
Step 2: Differentiate props from state and methods
Props are not for storing state or styling; they are inputs like function arguments.
Final Answer:
Data passed from a parent component to a child component -> Option A
Quick Check:
Props = Parent to child data [OK]
Hint: Props are like function arguments passed down [OK]
Common Mistakes:
Confusing props with state
Thinking props change inside the child
Believing props are used for styling
2. Which of the following is the correct way to pass a prop named title with value "Hello" to a component Greeting?
easy
A.
B.
C.
D.
Solution
Step 1: Recognize JSX prop syntax
Props are passed as attributes with key-value pairs inside JSX tags, using quotes for strings.
Step 2: Check each option's syntax
correctly uses title="Hello". misses the key, C uses unquoted value, D misses quotes.
Final Answer:
<Greeting title="Hello" /> -> Option D
Quick Check:
Props syntax = key="value" [OK]
Hint: Props look like HTML attributes with quotes [OK]