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
React JS: properties
Passing Props to a Component
📘 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.
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:
JSONis a lightweight data interchange format that is easy to read and write for both humans and machines.propsis 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:
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:
lin.jsx
2 The children prop
React Children API (react.dev reference)
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
This is the same idea as:
Card.jsx
You then nest content inside Card like this:
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,
activeIndexis0, so the firstPanelreceivesisActive = trueand the second receivesisActive = false. - When
activeIndexchanges to1, the firstPanelreceivesfalseand the second receivestrue.
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
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:
2. State goes up. The child calls the parent’s handler:
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.
4. Accordion re-runs. In this new render, activeIndex is 1:
5. Props go down. The parent computes the new isActive values:
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.
See also: React JS: render cycle.



