Components and props

Topic 2 of 5 in JSX & components6 min readIntermediateFree course

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:

JSX
<CourseCard title="React" lessons={49} level="Intermediate" />
  • Strings can use quotes: title="React".
  • Everything else uses curly braces: numbers lessons={49}, booleans isNew={true}, arrays, objects, functions, even JSX.
  • A prop with no value is true: <CourseCard isNew /> is the same as isNew={true}.

Reading props

React calls your component with one argument: an object containing all the props.

JSX
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:

JSX
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:

JSX
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:

JSX
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:

JSX
<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

CourseList owns the data and passes title and level props down to three CourseCards. Props are read-only. A child communicates back up by calling a function prop, onSelect, which the parent passed 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:

JSX
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:

JSX
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:

JSX
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).

Exercise

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.

JavaScript
export function CourseCard(/* props */) {
  // your code here
}

export default function App() {
  // your code here
}

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.