Passing Data through Props
Props, short for properties, are the values a parent component passes to a child component, much like arguments passed to a function. You use them whenever the same component should show different data, for example one ProductCard component rendering many different products.
Sending and receiving props
You pass props as attributes in JSX. Strings can go in quotes; everything else, such as numbers, arrays, objects or functions, goes inside curly braces. The child receives all of them as a single object, which is usually destructured right in the parameter list.
function ProductCard({ name, price, inStock = true }) {
return (
<div className="card">
<h3>{name}</h3>
<p>Price: {price.toFixed(2)}</p>
{!inStock && <p className="muted">Out of stock</p>}
</div>
);
}
function ProductList() {
const products = [
{ id: 1, name: 'Keyboard', price: 899 },
{ id: 2, name: 'Mouse', price: 349, inStock: false },
];
return (
<section>
{products.map((p) => (
<ProductCard key={p.id} name={p.name} price={p.price} inStock={p.inStock} />
))}
</section>
);
}
The = true in the destructuring is a default value. When the parent leaves inStock out, the child treats the product as available.
Props flow one way
Data in React moves downward: parent to child. A child should treat its props as read-only. If ProductCard did props.price = 0, it would be changing data it does not own, and React would not know to re-render anything.
When the child needs to change something, the parent passes a function as a prop, and the child calls it. For example, the parent passes onAddToCart, the child's button calls onAddToCart(id), and the parent updates its own state. The new state then flows back down as fresh props.
Useful prop patterns
- Boolean shorthand. Writing
<Button disabled />is the same asdisabled={true}. - Spreading an object.
<ProductCard {...p} />passes every key ofpas a separate prop. It is convenient but can pass more than the child needs, so use it with care. - The children prop. Anything placed between a component's opening and closing tags arrives as
children, which is how wrapper components likeCardorModalwork.
Mistakes that trip people up
Quoting a number is a frequent one. price="899" passes a string, so price.toFixed(2) throws an error. Use price={899} to pass a real number.
Another is forgetting that key is special. React uses it to track list items and does not pass it to the component, so props.key is always undefined. If the child needs the id, pass it again under another name, like id={p.id}.
Finally, watch for misspelled prop names. If the parent sends productName but the child reads name, nothing errors; the value is simply undefined and the heading is blank.
When props get passed too deep
Sometimes a value has to travel through several components that do not use it, just to reach a deeply nested child. This is called prop drilling. For a few levels it is perfectly fine. When it becomes painful, React's Context or a state library can make the value available directly, which you will meet later.
Main idea: props are inputs. Parents decide them, children read them, and changes go back up through callback props.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.