What are Props?
Props are arguments passed into React components. Props are passed to components via HTML attributes. The word "props" is short for properties.
Passing Props
You pass props to a component using attribute syntax:
function Car(props) {
return <h2>I am a {props.brand}!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand="Ford" />
</>
);
}
In this example, brand="Ford" is a prop being passed to the Car component.
Accessing Props
Props are received as an object parameter in function components:
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage
<Welcome name="Sarah" />
Hello, Sarah!
Multiple Props
You can pass multiple props to a component:
function Car(props) {
return (
<div>
<h2>Brand: {props.brand}</h2>
<p>Model: {props.model}</p>
<p>Year: {props.year}</p>
<p>Color: {props.color}</p>
</div>
);
}
function Garage() {
return (
<Car
brand="Ford"
model="Mustang"
year={1964}
color="red"
/>
);
}
Props with Variables
You can pass variables as props using curly braces:
function Car(props) {
return <h2>I am a {props.brand}!</h2>;
}
function Garage() {
const carName = "Ford";
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand={carName} />
</>
);
}
Props with Objects
You can pass entire objects as props:
function Car(props) {
return (
<h2>
I am a {props.car.color} {props.car.brand} {props.car.model}!
</h2>
);
}
function Garage() {
const carInfo = {
brand: "Ford",
model: "Mustang",
color: "red"
};
return (
<>
<h1>My Garage</h1>
<Car car={carInfo} />
</>
);
}
Destructuring Props
A cleaner way to access props is using destructuring:
// Instead of this:
function Car(props) {
return <h2>I am a {props.brand}!</h2>;
}
// You can write this:
function Car({ brand }) {
return <h2>I am a {brand}!</h2>;
}
// Multiple props:
function Car({ brand, model, year, color }) {
return (
<div>
<h2>{brand} {model}</h2>
<p>Year: {year}, Color: {color}</p>
</div>
);
}
Props in Class Components
If your component is a class component, props are available on this.props:
class Car extends React.Component {
render() {
return <h2>I am a {this.props.brand}!</h2>;
}
}
// Usage
<Car brand="Ford" />
Default Props
You can set default values for props:
function Car({ brand = "Unknown", model = "N/A" }) {
return <h2>{brand} {model}</h2>;
}
// If no props are passed, defaults will be used
<Car /> // Renders: Unknown N/A
<Car brand="Ford" /> // Renders: Ford N/A
Props are Read-Only
Props are immutable - you cannot change them from within the component!
function Car(props) {
props.brand = "Tesla"; // ❌ Don't do this!
return <h2>{props.brand}</h2>;
}
Passing Functions as Props
You can also pass functions as props to allow child components to communicate back to parents:
function Button({ onClick, text }) {
return <button onClick={onClick}>{text}</button>;
}
function App() {
const handleClick = () => {
alert("Button clicked!");
};
return <Button onClick={handleClick} text="Click Me" />;
}
The children Prop
React has a special prop called children that contains anything between the opening and closing tags of a component:
function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
}
function App() {
return (
<Card>
<h2>My Card Title</h2>
<p>This is the card content.</p>
</Card>
);
}
Real-World Example
Here's a practical example using props:
function UserCard({ name, email, age, avatar }) {
return (
<div className="user-card">
<img src={avatar} alt={name} />
<h3>{name}</h3>
<p>Email: {email}</p>
<p>Age: {age}</p>
</div>
);
}
function App() {
return (
<div>
<UserCard
name="John Doe"
email="john@example.com"
age={30}
avatar="avatar1.jpg"
/>
<UserCard
name="Jane Smith"
email="jane@example.com"
age={25}
avatar="avatar2.jpg"
/>
</div>
);
}
Props Summary
- Props are used to pass data from parent to child components
- Props are read-only and immutable
- Props can be any JavaScript value: strings, numbers, objects, arrays, functions
- Use destructuring for cleaner code
- Props flow in one direction (parent → child)
What's Next?
Now that you understand how to pass data with props, you're ready to learn about State - how components can manage their own internal data!