Props in React
# Props in React
1. Introduction
Up until now, our components have been static. A<Header> component always displayed the exact same text. But the real power of React components lies in their reusability. How do we create a <Button> component that says "Login" in one place, but says "Submit" in another? We use Props. In this chapter, we will learn how to make components dynamic and flexible by passing data into them.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define what Props are in React.
- Pass data (strings, numbers, arrays, objects) from a Parent component to a Child component.
- Access and display props inside a component.
- Use ES6 destructuring to write cleaner prop code.
- Understand that props are read-only.
3. Beginner-Friendly Explanations
What are Props?
"Props" is short for properties. Think of them as arguments passed into a JavaScript function or attributes on an HTML tag. In standard HTML, you pass attributes to make tags unique:<img src="cat.jpg" alt="A cute cat" />
Here, src and alt are properties.
In React, you do the exact same thing with your custom components:
<UserProfile name="Alex" age="28" />
Here, name and age are Props.
The Flow of Data
In React, data flows in one direction: downward. A "Parent" component passes props down to a "Child" component. The child receives the data and renders it.4. Syntax Explanation
Step 1: Passing Props (Parent Component)
You pass props just like you add attributes to HTML. String values use quotes" ", while numbers, booleans, and JavaScript variables require curly braces { }.
```jsx id="ch6_ex1" // Example React component (Parent) function App() { return ( <div> <Greeting userName="Sarah" age={25} isOnline={true} /> <Greeting userName="John" age={32} isOnline={false} /> </div> ); }
jsx id="ch6_ex2" // Example React component (Child) function Greeting(props) { return ( <div className="card"> <h2>Hello, {props.userName}!</h2> <p>Age: {props.age}</p> </div> ); }
jsx id="ch6_ex3" // Example React component (Child with Destructuring) // Instead of (props), we write ({ userName, age }) const Greeting = ({ userName, age }) => { return ( <div className="card"> <h2>Hello, {userName}!</h2> <p>Age: {age}</p> </div> ); }
jsx id="ch6_proj1" // Example React component
// 1. The Child Component
const UserCard = ({ name, role, email, avatarUrl, isActive }) => {
return (
<div className="bg-white rounded-xl shadow-sm border border-gray-100 p-6 flex items-center gap-4 hover:shadow-md transition-shadow">
{/* Avatar with an online status indicator */}
<div className="relative">
<img
src={avatarUrl}
alt={name}
className="w-16 h-16 rounded-full object-cover border-2 border-gray-100"
/>
{/* Conditional rendering for the green/gray dot based on isActive prop */}
<span className={absolute bottom-0 right-0 w-4 h-4 rounded-full border-2 border-white ${isActive ? 'bg-green-500' : 'bg-gray-400'}}></span>
</div>
{/* User Info */} <div> <h3 className="text-lg font-bold text-gray-900">{name}</h3> <p className="text-sm font-medium text-blue-600">{role}</p> <p className="text-sm text-gray-500 mt-1">{email}</p> </div> </div> ); };
// 2. The Parent Component export default function TeamDirectory() { return ( <div className="max-w-3xl mx-auto p-8"> <h1 className="text-3xl font-black text-gray-800 mb-8">Our Team</h1> {/* Reusing the component 3 times with different data! */} <div className="grid gap-4"> <UserCard name="Alice Johnson" role="Lead Engineer" email="alice@techcorp.com" avatarUrl="https://i.pravatar.cc/150?img=1" isActive={true} /> <UserCard name="Mark Smith" role="UI Designer" email="mark@techcorp.com" avatarUrl="https://i.pravatar.cc/150?img=11" isActive={false} />
<UserCard
name="Dr. Sarah Lee"
role="Data Scientist"
email="sarah@techcorp.com"
avatarUrl="https://i.pravatar.cc/150?img=5"
isActive={true}
/>
</div>
</div>
);
}
``
12. Coding Challenges
Challenge 1: Create a WeatherWidget component that takes city (string) and temperature (number) as props. Render it in an App component for "New York" (72) and "London" (55).
13. MCQs with Answers
Q1: In React, data flow is:
A) Bi-directional (Up and Down)
B) Unidirectional (Parent to Child)
C) Unidirectional (Child to Parent)
D) Circular
*Answer: B*
Q2: Which syntax is correct for destructuring props in the function parameters?
A)
const Card = (props.title, props.desc) => {}
B) const Card = [title, desc] => {}
C) const Card = ({ title, desc }) => {}
D) const Card = {title, desc} => {}
*Answer: C*
14. Interview Questions
-
Q: Can a child component modify its own props? *(Answer: No, props are read-only. Modifying them violates React's pure function principles).*
-
Q: How do you pass a JavaScript variable as a prop instead of a hardcoded string?
15. FAQs
Can I pass an entire object as a prop instead of individual strings?
Yes! <UserCard userData={myUserObject} /> is perfectly valid. Inside the component, you would access it via props.userData.name.
16. Summary
Props (Properties) are how React components talk to each other. A parent passes data downwards to a child as attributes. The child receives that data via the props` object (or through destructuring) and uses it to render dynamic, customizable UI. Props are strictly read-only.