Props let you send information from one component to another in React. This helps components share data and work together.
Passing props to components in React
Introduction
Syntax
React
function ChildComponent(props) { return <div>{props.message}</div>; } function ParentComponent() { return <ChildComponent message="Hello!" />; }
Props are passed like HTML attributes when using a component.
Inside the child, props is an object holding all passed values.
Examples
React
function Welcome(props) { return <h1>Welcome, {props.name}!</h1>; } function App() { return <Welcome name="Alice" />; }
React
function Button(props) { return <button style={{ backgroundColor: props.color }}>{props.label}</button>; } function App() { return <Button label="Click me" color="blue" />; }
React
function AlertButton({ onAlert }) { return <button onClick={onAlert}>Alert</button>; } function App() { const showAlert = () => alert('Button clicked!'); return <AlertButton onAlert={showAlert} />; }
Sample Program
This app shows two greetings with different names by passing props to the Greeting component.
React
import React from 'react'; function Greeting(props) { return <h2>Hello, {props.user}!</h2>; } export default function App() { return ( <main> <Greeting user="Sam" /> <Greeting user="Lee" /> </main> ); }
Important Notes
Props are read-only inside the child component; do not try to change them.
Use destructuring like function Greeting({ user }) for cleaner code.
Always give props meaningful names to keep code clear.
Summary
Props send data from parent to child components.
They look like HTML attributes when used.
Inside the child, props are accessed as an object.
Practice
1. What is the main purpose of
props in React components?easy
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 CQuick 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
Solution
Step 1: Recall JSX prop syntax
Props are passed like HTML attributes:name="value"orname={expression}.Step 2: Check each option
uses correct string syntax. misses quotes around string. uses invalid quotes. uses colon which is invalid in JSX. Final Answer:
<Header title="Hello" /> -> Option BQuick Check:
Props look like HTML attributes [OK]
Hint: Use quotes for string props in JSX [OK]
Common Mistakes:
- Omitting quotes for string values
- Using colons instead of equals
- Adding extra quotes around prop names
3. What will be rendered by this React component?
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage:
<Greeting name="Alice" />medium
Solution
Step 1: Understand how props are used inside the component
The component receives props and accessesprops.nameto display the name.Step 2: Check the usage of the component
The component is called withname="Alice", soprops.nameis "Alice".Final Answer:
<h1>Hello, Alice!</h1> -> Option AQuick Check:
props.name = Alice [OK]
Hint: Props values replace placeholders inside JSX [OK]
Common Mistakes:
- Printing literal {props.name} instead of value
- Forgetting to pass the prop
- Assuming props is undefined
4. Identify the error in this code snippet:
function Welcome(props) {
return <h2>Welcome, {props.user}!</h2>;
}
// Usage:
<Welcome user=John />medium
Solution
Step 1: Check the prop usage in JSX
In JSX, string prop values must be quoted, e.g.,user="John".Step 2: Identify the error in usage
The code usesuser=Johnwithout quotes, causing a syntax error.Final Answer:
Missing quotes around string prop value 'John' -> Option AQuick Check:
String props need quotes [OK]
Hint: Always quote string props in JSX [OK]
Common Mistakes:
- Omitting quotes for string props
- Using lowercase component names
- Leaving JSX tags unclosed
5. Given this parent and child component code, what will be the output?
function Child({ message }) {
return <p>Message: {message || 'No message'}</p>;
}
function Parent() {
const msg = '';
return <Child message={msg} />;
}hard
Solution
Step 1: Analyze the prop value passed
The parent passesmsgwhich is an empty string''as themessageprop.Step 2: Understand the child component's rendering logic
The child uses{message || 'No message'}. Since empty string is falsy, it falls back to 'No message'.Final Answer:
<p>Message: No message</p> -> Option DQuick Check:
Falsy empty string triggers fallback [OK]
Hint: Falsy props like '' use fallback values in JSX [OK]
Common Mistakes:
- Assuming empty string is truthy
- Expecting empty string to display as is
- Thinking missing prop causes error here
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
