Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 132 ยท Components and Props

Props

Learn how React props work - passing values, destructuring, default values, read-only rules, functions as props, spreading and the children prop - with live code.

A component that always shows the same thing is not very useful. Props (short for properties) are how a parent component sends data to a child. If a component is a function, props are its arguments: you decide at each use what the name, the colour or the click handler should be, and the component stays reusable.

This lesson covers how to pass props, how to read and destructure them, default values, the rule that props are read-only, passing functions and objects, spreading, and a first look at the special children prop. It builds on Your First Components. Later lessons build on this one: Children and Composition and Lifting State Up.

Passing and reading props

You pass props to a component with attribute syntax, exactly like HTML attributes. React gathers them into one object and passes it as the function's first argument.

Props arrive as one object

The console shows two objects, {label: "New", color: "seagreen"} and {label: "Sale", color: "crimson"}: one object per use. That is the whole mechanism. Everything else in this lesson is convenience on top of it.

Destructuring

Writing props.label and props.color over and over gets noisy. Almost every component destructures the object right in the parameter list:

JSX
function Badge({ label, color }) {
  return <span style={{ background: color }}>{label}</span>;
}

Destructuring also documents the component: the signature tells readers which props it expects. (If the syntax is unfamiliar, see The JavaScript You Need for React.)

Which values can be props?

Any JavaScript value. Strings can be written in quotes, but everything else goes inside curly braces, which mean "a JavaScript expression goes here":

Strings, numbers, booleans, arrays and objects as props

Note dimensions={{ width: 20, height: 45 }}: the outer braces open an expression, the inner ones are the object literal. They are not a special "double brace" syntax. A prop written with no value, such as <Product inStock />, means inStock={true}.

Default values

A prop the parent did not pass is undefined. Give it a default with a default value in the destructuring:

Default prop values

All three buttons work. The third passes undefined explicitly, and the default still applies, because defaults replace undefined. They do not replace null, 0 or '': label={null} renders an empty button. Old code used Component.defaultProps; it was removed for function components in React 19, so use default values in the parameters.

Props are read-only

A component must never change its own props. Props are a snapshot of what the parent decided for this render. If a child needs a value that changes over time, it keeps that in state (State with useState); if the parent needs to change the value, the parent passes a new one on the next render.

Mutating a prop object is a bug even when it appears to work. React's development build freezes props objects to catch it, so the assignment below fails right away.

Mutating props throws an error (fails on purpose)

The page shows quiet and the console prints assignment threw: TypeError: Cannot assign to read only property 'text' of object '#<Object>'. The object belongs to the parent's render, so changing it breaks the idea that a component's output depends only on its inputs, and repeated renders would act on already-modified data. Treat props as constants. The clean way to "change" a prop is to derive a new value:

JSX
function Shout({ text }) {
  const loud = text.toUpperCase(); // a new value, props untouched
  return <p>{loud}</p>;
}

The same rule applies to the values inside props: if you receive an array, do not push into it or sort() it in place (sort mutates). Copy first, or use non-mutating methods. Keeping Components Pure explains the idea in depth.

Passing functions as props

Props are not only data. A parent often passes a function, so the child can tell the parent that something happened. The child calls it; the parent decides what to do.

A child reports back through a function prop

LikeButton has no idea what "like" means; it only knows to call onLike. The state lives in Post. Name function props with an on prefix (onLike, onClose, onSelect) and the handler you pass with handle (handleLike). This "data flows down, events flow up" pattern is the backbone of React design. Passing the function reference (onLike={handleLike}) is different from calling it (onLike={handleLike()} would run it during render, a classic bug).

Spreading props

When you have an object whose keys match a component's props, spread it with {...object}:

Spreading an object into props

Spreading is handy for forwarding props to another component or a DOM element (<button {...rest}>). Use it with care: it hides which props are really passed, and a later spread silently overrides an earlier prop with the same name (<Avatar size={32} {...person} /> lets person.size win). A frequent idiom picks a few props and collects the rest:

JSX
function Card({ title, ...rest }) {
  return <section {...rest}><h2>{title}</h2></section>;
}

The special children prop

Anything you write between a component's opening and closing tags arrives as the prop children.

children is a prop like any other

Panel knows nothing about its contents, which makes it flexible. The next lesson, Children and Composition, turns this into a full design technique.

Props are not shared and not magic

Each component instance receives its own props object. A few facts professionals keep in mind:

  • A prop change triggers a re-render of the child, because the parent rendered again and called the child with new arguments. When the parent re-renders, children re-render too, even with identical props; skipping that is the job of memo (memo and Referential Equality).
  • Objects and functions created inline are new on every render. <Chart options={{ legend: true }} /> passes a new object each time. Harmless most of the time; matters for memoization and effect dependencies.
  • key and ref are special. key is never delivered to the component. In React 19 ref is an ordinary prop for function components.
  • Prop drilling. Passing a prop through many layers that do not use it gets tedious. It is fine for two or three levels; after that consider composition or context.
  • Keep prop lists small. A component with twelve props probably does too much, or should take an object or children instead. See Designing Components.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A parent writes <Badge label="New" color="green" />. What does the Badge function receive?

    React collects all attributes into a single props object and passes it as the first argument.
  2. 2. What is the value of label inside function Button({ label = 'Click' }) when the parent passes label={null}?

    Default values only replace undefined. null is a value, so the default is not used.
  3. 3. Which of these is the correct way to react to a prop change by transforming a text prop?

    Props are read-only. Derive a new local value instead of changing the object you were given.
  4. 4. What is wrong with <LikeButton onLike={handleLike()} />?

    handleLike() runs immediately while rendering. To pass the function itself, write onLike={handleLike}.
  5. 5. In <Panel title="A"><p>Hi</p></Panel>, how does Panel read the paragraph?

    Whatever is between the opening and closing tags is delivered as the children prop.

Summary

  • Props are the arguments of a component, delivered as one object. Destructure them in the parameter list.
  • Strings can use quotes; every other value goes in {curly braces}.
  • Defaults come from parameter default values and replace only undefined.
  • Props are read-only. Derive new values; never mutate props or the arrays and objects inside them.
  • Pass functions (onSomething) so children can report events to the parent.
  • {...object} spreads many props at once; later props override earlier ones.
  • children holds whatever is nested between the tags.

Next: Children and Composition