TExcelTAD
Home › React › React Props

React Props

Props (short for properties) are how a parent component passes data to a child. You write them like HTML attributes, and the child receives them as a single object — its first function parameter.

Passing and reading props

jsx · try it yourself
function Dish(props) {
  return (
    <li>
      {props.name} — {props.price.toLocaleString('en-US')}₫
    </li>
  );
}

export default function App() {
  return (
    <ul>
      <Dish name="Beef pho" price={55000} />
      <Dish name="Spring rolls" price={35000} />
      <Dish name="Iced coffee" price={25000} />
    </ul>
  );
}

String props can use quotes; numbers, booleans, objects, arrays and functions need braces: price={55000}.

Destructuring props

Most React code destructures props right in the parameter list. It's shorter and shows at a glance which props the component expects. You can also give default values:

jsx · try it yourself
function Badge({ label, color = 'gray', rounded = true }) {
  return (
    <span
      style={{
        background: color,
        color: 'white',
        padding: '2px 10px',
        marginRight: 6,
        borderRadius: rounded ? 999 : 2,
      }}
    >
      {label}
    </span>
  );
}

export default function App() {
  return (
    <p>
      <Badge label="new" color="green" />
      <Badge label="sale" color="crimson" rounded={false} />
      <Badge label="default" />
    </p>
  );
}

A default is used only when the prop is missing or undefined — passing null or 0 does not trigger it.

Any value can be a prop

Prop value Example
string title="Hello"
number count={3}
boolean disabled={true} or just disabled
object user={{ name: 'An', age: 30 }}
array tags={['react', 'js']}
function onSelect={handleSelect}
JSX icon={<StarIcon />}
jsx · try it yourself
function UserCard({ user, tags, icon, highlighted }) {
  return (
    <div style={{ border: highlighted ? '2px solid gold' : '1px solid #ccc', padding: 12, borderRadius: 8, marginBottom: 8, width: 260 }}>
      <div style={{ fontSize: 24 }}>{icon}</div>
      <strong>{user.name}</strong>, {user.age}
      <div>{tags.join(' · ')}</div>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <UserCard user={{ name: 'An', age: 30 }} tags={['react', 'design']} icon={<span>🎨</span>} highlighted />
      <UserCard user={{ name: 'Binh', age: 25 }} tags={['backend']} icon="🛠️" />
    </div>
  );
}

Writing a prop without a value (highlighted) is shorthand for highlighted={true}.

Props are read-only

A component must never change its own props. Props belong to the parent; treat them like function arguments you're not allowed to reassign. When a value needs to change over time, it belongs in state (usually in the parent), and the parent passes the new value down.

jsx
function Price({ amount }) {
  amount = amount * 0.9;   // ❌ don't modify props
  const discounted = amount * 0.9;   // ✅ compute a new value instead
}

Props change when the parent re-renders

When the parent's state changes, it re-renders and passes new props, and the child shows the new values automatically:

jsx · try it yourself
import { useState } from 'react';

function Thermometer({ celsius }) {
  const fahrenheit = (celsius * 9) / 5 + 32;
  const color = celsius >= 30 ? 'tomato' : celsius <= 10 ? 'steelblue' : 'seagreen';
  return (
    <p style={{ color, fontSize: 22 }}>
      {celsius}°C = {fahrenheit.toFixed(1)}°F
    </p>
  );
}

export default function App() {
  const [temp, setTemp] = useState(22);
  return (
    <div>
      <input type="range" min={-10} max={45} value={temp} onChange={(e) => setTemp(Number(e.target.value))} />
      <Thermometer celsius={temp} />
    </div>
  );
}

Passing functions as props

Children talk back to parents by calling a function the parent passed in. By convention these props are named onSomething:

jsx · try it yourself
import { useState } from 'react';

function VoteButton({ label, onVote }) {
  return <button onClick={() => onVote(label)} style={{ marginRight: 6 }}>{label}</button>;
}

export default function App() {
  const [last, setLast] = useState('nobody yet');
  return (
    <div>
      <p>Favourite fruit?</p>
      <VoteButton label="🍎 Apple" onVote={setLast} />
      <VoteButton label="🥭 Mango" onVote={setLast} />
      <VoteButton label="🍌 Banana" onVote={setLast} />
      <p>Last vote: {last}</p>
    </div>
  );
}

Spreading props

If you already have an object with the right keys, {...obj} passes all of them at once. Use it sparingly; explicit props are easier to read.

jsx
const product = { name: 'Bag', price: 30 };
<Dish {...product} />   // same as <Dish name="Bag" price={30} />

Note: key and ref are special and are not passed to your component as ordinary props (in React 19, ref is available as a prop on function components; key never is).

Test yourself

How does a child component receive props?

  1. Through a global variable
  2. As an object in its first parameter
  3. By calling useProps()
  4. Through this.props only

What does <Toggle on /> pass for the on prop?

  1. "on"
  2. undefined
  3. true
  4. null

A child needs to tell its parent that a button was clicked. What is the usual approach?

  1. Modify the props object
  2. The parent passes a function prop the child calls
  3. Use document.dispatchEvent
  4. Return the value from the child component