Components and props
Overview
A CourseCard that always shows “React, 49 lessons” isn’t very useful. Props (short for properties) let a parent component pass data into a child, so the same component can display different content — just like arguments make a function reusable.
Passing props
You pass props like HTML attributes:
<CourseCard title="React" lessons={49} level="Intermediate" />- Strings can use quotes:
title="React". - Everything else uses curly braces: numbers
lessons={49}, booleansisNew={true}, arrays, objects, functions, even JSX. - A prop with no value is
true:<CourseCard isNew />is the same asisNew={true}.
Reading props
React calls your component with one argument: an object containing all the props.
function CourseCard(props) {
return (
<article>
<h2>{props.title}</h2>
<p>{props.lessons} lessons · {props.level}</p>
</article>
);
}Almost everyone destructures the props in the parameter list instead — it documents which props the component uses and reads more cleanly:
function CourseCard({ title, lessons, level }) {
return (
<article>
<h2>{title}</h2>
<p>{lessons} lessons · {level}</p>
</article>
);
}Default values
Give optional props a default with destructuring defaults:
function CourseCard({ title, lessons, level = 'Beginner', isNew = false }) {
// level is 'Beginner' when the prop is missing or undefined
}The default applies only when the prop is missing or undefined — not when it’s null, 0 or ''.
Passing objects and spreading props
You can pass a whole object:
const course = { id: 7, title: 'React', lessons: 49, level: 'Intermediate' };
<CourseCard course={course} />
function CourseCard({ course }) {
return <h2>{course.title}</h2>;
}Or spread its properties as individual props:
<CourseCard {...course} /> // same as title="React" lessons={49} level="Intermediate" id={7}Spreading is handy for forwarding props, but use it sparingly: explicit props make it obvious what a component receives.
Props flow down
React uses one-way data flow: data moves from parents to children through props. A child can’t reach up and change its parent’s data. This makes apps predictable: to find where a value comes from, you follow the props up the tree.
When a child needs to tell its parent something — “the user clicked me” — the parent passes down a function, and the child calls it:
function CourseList({ courses }) {
function handleSelect(id) {
console.log('Selected course', id);
}
return courses.map((course) => (
<CourseCard key={course.id} course={course} onSelect={handleSelect} />
));
}
function CourseCard({ course, onSelect }) {
return (
<article>
<h2>{course.title}</h2>
<button onClick={() => onSelect(course.id)}>View course</button>
</article>
);
}By convention, function props that respond to events are named onSomething (onSelect, onDelete), and the functions that handle them are named handleSomething.
Props are read-only
A component must never change its own props:
function CourseCard({ course }) {
course.title = course.title.toUpperCase(); // ✗ mutating a prop
return <h2>{course.title}</h2>;
}That object belongs to the parent — mutating it silently changes the parent’s data and breaks React’s assumptions. If you need a modified value, compute a new one:
function CourseCard({ course }) {
const heading = course.title.toUpperCase(); // ✓ derived value
return <h2>{heading}</h2>;
}When something needs to change over time in response to the user, that’s state, covered in the next chapter. Props are like a function’s arguments; state is like a component’s memory.
Props change between renders
Props aren’t fixed forever. When a parent re-renders with different values, the child receives the new props and renders again. A component just reflects whatever props it receives right now — don’t copy props into variables expecting them to stay in sync, and don’t copy them into state without a good reason (you’ll see why in Structuring state).
Thinking in props
When designing a component, ask: what varies between its uses? Those are its props. A Button varies in label, style variant and what happens on click — so its props might be children, variant and onClick. Everything that stays the same lives inside the component.
Tip: if a component takes more than about seven props, it may be doing too much. Consider splitting it, or passing content with
children(next lesson).
A reusable course card
Write a CourseCard component that accepts title, lessons, level (default 'Beginner') and isNew (default false) props. It renders the title in an <h2>, “N lessons · level” in a <p>, and a <span className="badge">New</span> only when isNew is true. Render three cards from App with different props, including one that relies on the defaults.
export function CourseCard(/* props */) {
// your code here
}
export default function App() {
// your code here
}export function CourseCard({ title, lessons, level = 'Beginner', isNew = false }) {
return (
<article className="card">
<h2>
{title} {isNew && <span className="badge">New</span>}
</h2>
<p>
{lessons} lessons · {level}
</p>
</article>
);
}
export default function App() {
return (
<main>
<CourseCard title="React" lessons={49} level="Intermediate" isNew />
<CourseCard title="Node.js" lessons={54} level="Intermediate" />
<CourseCard title="HTML basics" lessons={12} />
</main>
);
}Summary
- Props are the inputs to a component: React passes them as a single object argument.
- Destructure props in the function signature and give optional props default values.
- Pass strings in quotes and everything else (numbers, booleans, objects, functions) in curly braces.
- Props are read-only — a component must never modify its own props.
- Data flows down through props; children communicate upwards by calling functions passed as props.