Props — Passing Data to Components
⏱ 15 min read
Props (short for "properties") are how you pass data from a parent component to a child component. They flow in ONE direction — from parent to child. You cannot pass props upward.
Passing Props:
<UserCard name="Alice" age={25} isAdmin={true} />
String values use quotes. Everything else (numbers, booleans, arrays, objects, functions) uses curly braces.
Receiving Props:
function UserCard(props) {
return <h2>{props.name}</h2>;
}
// Better — destructure directly:
function UserCard({ name, age, isAdmin }) {
return <h2>{name}</h2>;
}
Default Props — set fallbacks right in the function signature:
function Button({ label = 'Click me', color = 'blue', disabled = false }) { ... }
The children Prop:
Whatever you put between a component's opening and closing tags becomes props.children:
<Card><p>This is the card content</p></Card>
function Card({ children, title }) {
return <div><h2>{title}</h2>{children}</div>;
}
Props are READ-ONLY:
Never modify props inside a component. Props belong to the parent. If you need to change data, use state (Lesson 3).
Prop Types — common patterns:
- string: name="Alice"
- number: age={25}
- boolean: isActive={true} or just isActive (shorthand for isActive={true})
- array: items={[1, 2, 3]}
- object: user={{ name: 'Alice', age: 25 }}
- function: onClick={handleClick}
- JSX: icon={<StarIcon />}
Spreading Props:
const userProps = { name: 'Alice', age: 25 };
<UserCard {...userProps} /> — same as <UserCard name="Alice" age={25} />
Passing Props:
<UserCard name="Alice" age={25} isAdmin={true} />
String values use quotes. Everything else (numbers, booleans, arrays, objects, functions) uses curly braces.
Receiving Props:
function UserCard(props) {
return <h2>{props.name}</h2>;
}
// Better — destructure directly:
function UserCard({ name, age, isAdmin }) {
return <h2>{name}</h2>;
}
Default Props — set fallbacks right in the function signature:
function Button({ label = 'Click me', color = 'blue', disabled = false }) { ... }
The children Prop:
Whatever you put between a component's opening and closing tags becomes props.children:
<Card><p>This is the card content</p></Card>
function Card({ children, title }) {
return <div><h2>{title}</h2>{children}</div>;
}
Props are READ-ONLY:
Never modify props inside a component. Props belong to the parent. If you need to change data, use state (Lesson 3).
Prop Types — common patterns:
- string: name="Alice"
- number: age={25}
- boolean: isActive={true} or just isActive (shorthand for isActive={true})
- array: items={[1, 2, 3]}
- object: user={{ name: 'Alice', age: 25 }}
- function: onClick={handleClick}
- JSX: icon={<StarIcon />}
Spreading Props:
const userProps = { name: 'Alice', age: 25 };
<UserCard {...userProps} /> — same as <UserCard name="Alice" age={25} />
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress