📖 Scenario: You are building a simple React app where a parent component holds a message and passes it to a child component to display.
🎯 Goal: Create a parent component that stores a message string and passes it as a prop to a child component. The child component should display the message inside a paragraph.
📋 What You'll Learn
Create a parent component called Parent with a message state variable
Create a child component called Child that accepts a message prop
Pass the message from Parent to Child as a prop
Render the Child component inside Parent
Display the message inside a <p> tag in Child
💡 Why This Matters
🌍 Real World
Passing data from parent to child components is a fundamental pattern in React apps for building reusable UI parts.
💼 Career
Understanding parent-child data flow is essential for React developers to manage component communication and state effectively.
Progress0 / 4 steps
1
Create the Parent component with a message state
Create a React functional component called Parent. Inside it, create a state variable called message using useState and set its initial value to "Hello from Parent".
React
Hint
Use useState to create the message state inside Parent.
2
Create the Child component that accepts a message prop
Create a React functional component called Child that accepts a prop named message. Inside the component, return a <p> element that displays the message prop.
React
Hint
Use destructuring in the function parameter to get the message prop.
3
Pass the message prop from Parent to Child
Inside the Parent component's return statement, render the Child component and pass the message state as a prop named message.
React
Hint
Use JSX to render <Child message={message} /> inside Parent.
4
Export the Parent component as default
Add an export statement to export the Parent component as the default export of the module.
React
Hint
Use export default Parent; at the end of the file.
Practice
(1/5)
1. In React, how does a parent component send data to its child component?
easy
A. By importing the child's variables
B. By passing data as props to the child component
C. By using the child's setState method
D. By directly modifying the child's state
Solution
Step 1: Understand data flow in React
React uses one-way data flow where parents send data down to children.
Step 2: Identify how data is passed
Parents pass data to children using props, which are like function arguments.
Final Answer:
By passing data as props to the child component -> Option B
Quick Check:
Parent to child data flow = props [OK]
Hint: Remember: props flow down from parent to child [OK]
Common Mistakes:
Trying to change child's state directly from parent
Using child's setState from parent
Importing child's variables instead of passing props
2. Which of the following is the correct syntax to pass a prop named title with value "Hello" from a parent to a child component Child?
easy
A. <Child>title="Hello"</Child>
B. <Child {title: "Hello"} />
C. <Child title="Hello" />
D. <Child>{title: "Hello"}</Child>
Solution
Step 1: Recall JSX prop syntax
Props are passed as attributes inside the component tag, like HTML attributes.
Step 2: Match correct syntax
The correct way is <Child title="Hello" /> which sets prop title to "Hello".
Final Answer:
<Child title="Hello" /> -> Option C
Quick Check:
Props look like HTML attributes [OK]
Hint: Props look like HTML attributes inside component tags [OK]
Common Mistakes:
Using object syntax inside JSX tags
Placing props inside children instead of attributes
Using curly braces incorrectly for props
3. Given the code below, what will be rendered inside the <Child /> component?
function Parent() {
const message = "Hi there!";
return <Child text={message} />;
}
function Child(props) {
return <h1>{props.text}</h1>;
}
medium
A.
Hi there!
B.
text
C.
{message}
D. Nothing will render
Solution
Step 1: Identify prop passed from Parent
Parent passes prop named text with value "Hi there!" to Child.
Step 2: Check Child rendering
Child renders <h1> with content from props.text, which is "Hi there!".
Final Answer:
<h1>Hi there!</h1> -> Option A
Quick Check:
props.text = "Hi there!" renders inside <h1> [OK]
Hint: Child renders what parent sends via props [OK]
Common Mistakes:
Confusing prop name with string literal
Expecting variable name to render instead of value
Thinking nothing renders without explicit return
4. What is wrong with the following code that tries to pass data from parent to child?
function Parent() {
const data = "Hello";
return <Child data=data />;
}
function Child(props) {
return <div>{props.data}</div>;
}
medium
A. Parent should use state instead of variable
B. Child component cannot receive props named data
C. Child must destructure props before use
D. The prop value should be inside curly braces: data={data}
Solution
Step 1: Check JSX prop syntax
In JSX, to pass a JavaScript variable as prop value, use curly braces.
Step 2: Identify error in code
Code uses data=data without braces, so it passes string "data" instead of variable value.
Final Answer:
The prop value should be inside curly braces: data={data} -> Option D
Quick Check:
JSX props need curly braces for variables [OK]
Hint: Use curly braces to pass variables as props [OK]
Common Mistakes:
Omitting curly braces around variables in JSX props
Thinking child cannot receive certain prop names
Believing state is required to pass props
5. You want to pass a list of items from a parent to a child component and render them as a list. Which approach correctly implements this parent-child data flow?