October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Pass Values to Components in React

Pass data to a React component through JSX props, then read it from the child’s props object or destructure it. See defaults, children, callbacks, forwarding, and when Context helps.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass a value to a React component by putting it on the component’s JSX tag as a prop, then read it in the child from its props object. For example, <Avatar size={100} /> passes the number 100 as the size prop.

Pass a value with a JSX prop

A parent supplies props as attributes on a child component’s JSX tag. A function component receives those values together in one props object. You can use that object directly or destructure the properties in the function parameter; destructuring is shorthand for reading the same object.

function Profile() {
  const person = { name: 'Lin Lanying', imageId: '1bX5QH6' };
  return <Avatar person={person} size={100} />;
}

function Avatar({ person, size }) {
  return <img alt={person.name} width={size} height={size} />;
}

In this example, person is an object and size is a number. The braces in JSX evaluate JavaScript expressions, so person={person} passes the variable’s value. To pass an object literal directly, use an outer pair of braces for the JavaScript expression and inner braces for the object: <Avatar person={{ name: 'Lin' }} />.

Props can hold strings, numbers, objects, arrays, and functions, as well as other JavaScript values. See React’s Passing Props to a Component guide for more examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose defaults for optional props

If a component should use a value when a prop is omitted, set a default while destructuring:

function Avatar({ size = 100 }) {
  return <img width={size} height={size} />;
}

This default is used when size is missing or undefined. It does not apply when the caller explicitly passes null or 0; those values remain null and 0.

Pass nested content with children

When a component wraps content chosen by its caller, put that content between the component’s opening and closing tags. React makes the nested JSX available to the wrapper as its children prop:

function Card({ children }) {
  return <section className="card">{children}</section>;
}

function Profile() {
  return (
    <Card>
      <Avatar size={100} />
    </Card>
  );
}

This pattern is useful for layout and wrapper components that should not need to know which specific content they contain.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Forward props when another component needs them

You can spread an existing props object onto a component to forward its properties:

function Profile({ avatarProps }) {
  return <Avatar {...avatarProps} />;
}

Forwarding can reduce repetitive code, but it can also make the receiving component’s inputs less obvious. Prefer explicit attributes when they make the child’s interface clearer at the call site.

Let a child report interactions through a callback

Props can include functions. A parent can pass a callback so a child can report an interaction; the parent can then update its own state and render the child again with new prop values. The child calls the callback rather than changing a received prop.

function Parent() {
  const [count, setCount] = useState(0);
  return <Counter count={count} onIncrement={() => setCount(count + 1)} />;
}

function Counter({ count, onIncrement }) {
  return <button onClick={onIncrement}>Count: {count}</button>;
}

Props are read-only snapshots: React’s documentation says, “Props are read-only snapshots in time: every render receives a new version of props.” The parent supplies changed values on a later render rather than the child mutating its props.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose between explicit props, spreading, and Context

Use the approach that matches how widely the value is needed and how visible you want the component interface to be.

Approach Best fit Trade-off
Explicit props A small, local set of values for a particular child. The call site clearly shows which values the child receives.
Spread forwarding Passing an existing props object through to another component. Can reduce repetitive forwarding, but may obscure the receiving component’s inputs.
Context A value needed by many descendants, especially when passing it through intermediate components becomes inconvenient. Avoids threading the value through each intermediate level, but is less direct than a local prop for one child.

Start with ordinary props for parent-to-child data flow. Consider Context when the same information is needed broadly or across many tree levels.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.