React JS: properties

Passing Props to a Component

reactjs
props
description
Author

albertprofe

Published

Tuesday, June 1, 2021

Modified

Friday, November 20, 2026

📘 Props

React components use props (props stands for properties) to communicate with each other.

Every parent component can pass some information to its child components by giving them props.

Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions.


Props are properties passed to a child component that can hold many data types (e.g. array, object, number, function, others).

Props are properties passed to a child component that can hold many data types (e.g. array, object, number, function, others).

In React JS, data flows in one direction, from Parent to Child. This helps components to be simple and predictable.

“Simplicity is the ultimate sophistication.” -Leonardo da Vinci

For state, the render cycle, and immutability, see React JS: render cycle.

1 Overview

In React.js, “props” (short for “properties”) are used to pass data from one component to another. Props are passed as an object (which is similar to a JSON object) and contain key-value pairs of data:

  • JSON is a lightweight data interchange format that is easy to read and write for both humans and machines.
  • props is a reserved keyword that refers to an object containing all the properties (or props) passed to a component.

For example, let’s say we have a component called MyComponent that receives an object named person as a prop. This person object might look like this:

const person = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  }
};

In this case, the person object contains some key-value pairs and another object. We can then pass this person object as a prop to our MyComponent component like this:

<MyComponent person={person} />

Inside MyComponent, we can access the person prop using the props object like this:

function MyComponent(props) {
  console.log(props.person.name); // 'John'
  console.log(props.person.age); // 30
  console.log(props.person.address.street); // '123 Main St'
  // ...
}

1.1 Example1

const car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2022,
  color: 'blue'
};

const person = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  }
};

<MyComponent person={person} car={car} />

function MyComponent(props) {
  console.log(props.person.name); // 'John'
  console.log(props.person.age); // 30
  console.log(props.person.address.street); // '123 Main St'

  console.log(props.car.make); // 'Toyota'
  console.log(props.car.model); // 'Corolla'
  console.log(props.car.year); // 2022
  console.log(props.car.color); // 'blue'
  // ...
}

1.2 Example2

Use the same syntax as HTML attributes to send props into a component:

profile.jsx
function Avatar({ person, size }) {
  // person and size are available here
}
lin.jsx
export default function Lin() {
  return (
    <Avatar
      person={{ name: 'Lin Lanying', imageId: '1bX5QH6' }}
      size={100}
    />
  );
}
carla.jsx
export default function Carla() {
  return (
    <Avatar
      person={{ name: 'Carla Lan', imageId: 'KIU3QH1' }}
      size={60}
    />
  );
}

2 The children prop

React Children API (react.dev reference)

Alternatives to Children

React docs pitfall on the Children API

React docs pitfall on the Children API

Using Children is uncommon and can lead to fragile code. See common alternatives.

children is a special prop: whatever you nest between a component’s opening and closing tags is passed to that component as children.

A wrapper component receives that nested JSX and can render it inside its own markup.

Without destructuring, you read it from the props object: props.children.

With object destructuring, you can pull children (and any other props) out of the props object in the function parameter:

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

This is the same idea as:

Card.jsx
function Card(props) {
  return (
    <section>
      <h2>{props.title}</h2>
      {props.children}
    </section>
  );
}

You then nest content inside Card like this:

App.jsx
<Card title="Profile">
  <Avatar person={person} size={100} />
</Card>

3 Example: passing props down, state up

📘 Props down, state up

The parent owns the state. It passes data and handlers down as props. Children call the handler to lift the change back up.

This Accordion example shows it clearly.

Accordion holds activeIndex in state. It passes an isActive prop down to each Panel.

  • Initially, activeIndex is 0, so the first Panel receives isActive = true and the second receives isActive = false.
  • When activeIndex changes to 1, the first Panel receives false and the second receives true.

The Panel does not own that state. It only receives isActive. To open a panel, the child calls a handler passed down as a prop, and the parent updates activeIndex. That is props down, state up.

Sharing state between components

Initially, Accordion’s activeIndex is 0, so the first Panel receives isActive = true. When Accordion’s activeIndex changes to 1, the second Panel receives isActive = true instead.

Initially, Accordion’s activeIndex is 0, so the first Panel receives isActive = true. When Accordion’s activeIndex changes to 1, the second Panel receives isActive = true instead.

Accordion code annotated: the parent passes isActive and onShow down to each Panel (1, 2), and Panel destructures title, children, isActive and onShow.

Accordion code annotated: the parent passes isActive and onShow down to each Panel (1, 2), and Panel destructures title, children, isActive and onShow.

Here is the code that runs it, from Sharing state between components:

import { useState } from 'react';

export default function Accordion() {
  const [activeIndex, setActiveIndex] = useState(0); // parent owns the state

  return (
    <>
      <h2>Almaty, Kazakhstan</h2>
      <Panel
        title="About"
        isActive={activeIndex === 0}          // props down
        onShow={() => setActiveIndex(0)}      // handler down, state up
      >
        With a population of about 2 million, Almaty is Kazakhstan's largest city.
        From 1929 to 1997, it was its capital city.
      </Panel>
      <Panel
        title="Etymology"
        isActive={activeIndex === 1}          // props down
        onShow={() => setActiveIndex(1)}      // handler down, state up
      >
        The name comes from алма, the Kazakh word for "apple", often translated as "full of apples".
      </Panel>
    </>
  );
}

function Panel({ title, children, isActive, onShow }) {
  return (
    <section className="panel">
      <h3>{title}</h3>
      {isActive ? (
        <p>{children}</p>
      ) : (
        <button onClick={onShow}>Show</button>
      )}
    </section>
  );
}

3.1 From the click to the next render

Assume activeIndex is 0: the first Panel is open, the second shows a Show button. The user clicks Show on panel 2.

1. The click fires onShow. The button’s onClick is the onShow prop:

<button onClick={onShow}>Show</button>

2. State goes up. The child calls the parent’s handler:

onShow={() => setActiveIndex(1)}

3. React schedules a re-render. setActiveIndex(1) does not change activeIndex in the current render. It queues the update and React schedules a re-render of Accordion. That is the trigger in the render cycle.

setActiveIndex(1);

4. Accordion re-runs. In this new render, activeIndex is 1:

const [activeIndex, setActiveIndex] = useState(0);
// activeIndex is now 1

5. Props go down. The parent computes the new isActive values:

isActive={activeIndex === 0}  // false
isActive={activeIndex === 1}  // true

6. Each Panel re-renders with its new isActive:

function Panel({ title, children, isActive, onShow }) {
  // panel 1: isActive === false  ->  button
  // panel 2: isActive === true   ->  content
}

7. React commits the DOM. Only the differences are written to the page: panel 1 now shows its Show button, and panel 2 shows its content. That is the commit in the render cycle.

sequenceDiagram
  actor User
  participant Panel2 as Panel 2
  participant Accordion
  participant React
  User->>Panel2: click Show
  Panel2->>Accordion: onShow / setActiveIndex(1)
  Note right of Accordion: state up
  Accordion->>React: queue update, schedule render
  React->>Accordion: re-render, activeIndex is 1
  Accordion->>Panel2: isActive true
  Note right of Accordion: props down
  React->>React: commit DOM

See also: React JS: render cycle.

Back to top