Lesson 159 of 296
Props
What you will learn
- What props are
- Passing and reading props
- Typing props with TypeScript
- Default values
- The children prop
- Passing functions as props
- Why props are read-only
On this page
What are props?
Props (short for properties) are the inputs of a component, passed from its parent like HTML attributes. They let one component be reused with different data.
type GreetingProps = {
name: string;
times?: number;
};
function Greeting({ name, times = 1 }: GreetingProps) {
return <p>{`Hello ${name}! `.repeat(times)}</p>;
}
function GreetingList() {
return (
<>
<Greeting name="Sam" />
<Greeting name="Aisha" times={2} />
</>
);
}- The component receives one object with all props. Destructuring (Lesson 62) in the parameter list is the usual style.
- Strings can be written in quotes (
name="Sam"); everything else goes in braces (times={2},isActive={true},items={list}). - Default values use normal default parameter syntax (
times = 1).
Props are read-only
A component must never change its props. They belong to the parent. To change something over time, a component uses state (Lesson 160).
function Bad({ count }: { count: number }) {
count = count + 1; do not do this
}This keeps data flow predictable: data flows down from parent to child. This is called one-way data flow.
The children prop
Whatever you put between a component's opening and closing tags arrives as the children prop:
import type { ReactNode } from "react";
function Card({ title, children }: { title: string; children: ReactNode }) {
return (
<section className="card">
<h2>{title}</h2>
<div className="card-body">{children}</div>
</section>
);
}
function CardExample() {
return (
<Card title="Your order">
<p>3 items</p>
<button type="button">Checkout</button>
</Card>
);
}ReactNode is the type for anything React can render: elements, strings, numbers, null, arrays of these. children makes components flexible "wrappers" (Lesson 165, composition).
Passing functions as props
Children talk back up to parents by calling functions the parent passed down:
type ProductRowProps = {
name: string;
onAdd: (name: string) => void;
};
function ProductRow({ name, onAdd }: ProductRowProps) {
return (
<li>
{name} <button type="button" onClick={() => onAdd(name)}>Add</button>
</li>
);
}Naming convention: props for events start with on (onAdd, onSelect); the parent's handler functions often start with handle (handleAdd).
Spreading props
function PrimaryButton(props: { label: string; disabled?: boolean }) {
const { label, ...rest } = props;
return <button type="button" className="btn-primary" {...rest}>{label}</button>;
}{...rest} passes all remaining props through. Useful for wrapper components; use with care, because it can pass unexpected attributes to the DOM.
Prop drilling
When data must pass through many layers of components that do not use it, just to reach a deep child, it is called prop drilling. It is fine for two or three levels. For more, use composition (Lesson 165) or context (Lesson 169).
How to say it in an interview
"Props are a component's inputs, passed from the parent and read-only, which gives React its one-way data flow: data flows down, and children communicate up by calling functions passed as props, like onAdd. children holds whatever is nested inside the component, which makes wrapper components easy. I type props with a TypeScript type, use destructuring with defaults, and avoid deep prop drilling with composition or context."
Key words
| Word | Meaning |
|---|---|
| Props | The inputs passed to a component |
| One-way data flow | Data flows from parent to child |
| children | The content between a component's tags |
| ReactNode | The type of anything React can render |
| Prop drilling | Passing props through components that do not need them |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.