Props
Passing data between components, prop destructuring, default values, and prop types.
What you'll learn
- How to pass and receive props from parent to child
- Destructuring props for cleaner code
- Default prop values and the children prop
- Why props are read-only and the one-way data flow
Concept
What Are Props?
Props (short for properties) are the mechanism for passing data from a parent component to a child component. They are the inputs to a component, much like function arguments. Props are always passed down the component tree — from parent to child — and they are read-only. A component must never modify its own props. This one-way data flow is a core React principle that keeps data flow predictable and debugging tractable.
Destructuring Props
Instead of accessing props.name, props.age, you can destructure props in the function signature for cleaner code: function User({ name, age }) {}. This is the idiomatic React style. You can also provide default values right in the signature: function User({ name = "Guest", age = 0 }) {}. Destructuring makes it immediately clear what inputs a component expects.
The children Prop
The special children prop contains whatever is passed between the opening and closing tags of a component. <Card>Hello</Card> passes "Hello" as children to Card. This is how you create wrapper and layout components. The children prop is powerful because it lets a component be agnostic about its content — the parent decides what goes inside.
Props Are Read-Only
A component must treat props as immutable. If you need to change a value, the parent should manage the state and pass a new prop value down. This is called lifting state up. Attempting to mutate props directly will not work and will cause confusing bugs. The unidirectional data flow (parent → child) is what makes React applications predictable and easy to reason about.
Prop Types and Validation
In modern React with TypeScript, you define prop types with interfaces, giving you compile-time type checking and autocomplete. Without TypeScript, you can use PropTypes for runtime validation. Always document what props a component expects, their types, and which are required. This makes your components self-documenting and catches integration errors early.
Example
// Destructuring props with default values for clean, readable code.
function UserCard({ name = "Guest", role = "Member", avatar, isActive }) {
return (
<div className={isActive ? "user-card active" : "user-card"}>
<img src={avatar} alt={name} className="avatar" />
<div className="user-info">
<h4>{name}</h4>
<span className="role">{role}</span>
<span className="status">{isActive ? "Online" : "Offline"}</span>
</div>
</div>
);
}
// Parent passes data down as props — one-way data flow.
function App() {
return (
<UserCard name="Alice" role="Admin" isActive={true}
avatar="/avatars/alice.png" />
);
}
Props are destructured in the signature with defaults. The parent App passes data down. UserCard cannot modify these props — it only reads and renders them.
Try it
- JSON Formatter
Props are often objects — inspect and format them clearly.
- JSON to YAML
Convert prop-like JSON data to YAML for configuration files.
Common mistakes
The mistake
Trying to modify props directly inside a child component
The fix
Props are read-only. If the child needs to change data, the parent should own the state and pass a callback prop down. The child calls the callback, and the parent updates state.
The mistake
Passing too many individual props instead of an object
The fix
If a component takes many props, consider grouping related ones into an object. But avoid passing the entire parent state — pass only what the child needs.
Practice Props
9 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.