Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober 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 Scan×
Skip to content
World desk3 min

How to Pass Arguments to React Components

Pass values to React components with props, and use a function wrapper to supply arguments to event handlers when they run.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass data to a React component as props: <Greeting name="Ada" /> gives the component a name prop. To pass an argument to an event handler later, pass a function that supplies it: onClick={() => handleSelect(item.id)}. These are related but different tasks—one supplies component input; the other schedules a function call for an event.

Pass data to a component with props

JSX attributes on a custom component become fields on its props object. A function component receives that one object; you can access it directly or destructure the fields you need. React describes the role simply: “React components use props to communicate with each other.” See React’s guide to passing props.

function Greeting({ name }) {
  return <h1>Hello, {name}</h1>;
}

function App() {
  return <Greeting name="Ada" />;
}

Here, Greeting receives a props object whose name field is "Ada". The destructured form function Greeting({ name }) binds that field. By contrast, function Greeting(name) binds the entire props object to the variable name; it does not extract the name field.

Pass JavaScript values, not only strings

Use curly braces in JSX to pass a JavaScript expression, such as a number, object, array, or function. Quoted attributes are convenient for string values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function Avatar({ person, size = 100 }) {
  return (
    <img
      src={person.imageUrl}
      width={size}
      height={size}
      alt={person.name}
    />
  );
}

function Profile() {
  const person = { name: "Ada", imageUrl: "/ada.png" };
  return <Avatar person={person} size={80} />;
}

The size = 100 default applies when size is omitted or undefined. It does not replace null or 0.

Pass an argument to an event handler

If a click should call a function with a particular value, pass an arrow function as the handler. The arrow function runs when the event occurs, and calls your function with that value. React’s event guide emphasizes: “Event handlers must be passed, not called!” See Responding to Events.

function Toolbar({ item }) {
  function handleSelect(id) {
    console.log("Selected", id);
  }

  return (
    <button onClick={() => handleSelect(item.id)}>
      Choose
    </button>
  );
}

For a handler that needs no extra argument, pass the function itself: onClick={handleClick}. Avoid onClick={handleClick()} when you mean to handle a later click: that expression invokes the function during rendering and passes its return value instead of a handler. The arrow wrapper defers the call and binds item.id for that event.

Forward a handler through a custom component

A parent can pass a function as a prop, and a child can attach it to a built-in element. The custom prop name is up to the component; the DOM button uses onClick.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function Button({ onClick, children }) {
  return <button onClick={onClick}>{children}</button>;
}

function Toolbar({ item }) {
  function handleSelect(id) {
    console.log("Selected", id);
  }

  return (
    <Button onClick={() => handleSelect(item.id)}>
      Select
    </Button>
  );
}

When a handler needs the browser event as well, React supplies the event to the handler. Define the handler to accept it where needed, and deliberately decide how any additional value is supplied. The React reference for common DOM components documents built-in event props.

Choose a clear prop shape

Approach Example Good fit
Separate named props <Avatar person={person} size={80} /> Inputs should be explicit and easy to see at the call site.
Grouped object <Avatar person={person} /> Related values already belong together as one object.
Nested JSX as children <Card><Avatar person={person} /></Card> A wrapper should display supplied content without needing to inspect its details.

Nested JSX is received through the children prop:

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

When passing values onward, you can list them explicitly so the receiving component’s inputs remain visible:

function Profile({ person, size }) {
  return <Avatar person={person} size={size} />;
}

JSX spread can forward a set of props, as in <Avatar {...props} />. Use it selectively: if components repeatedly forward props they do not use, composing them with children may make the relationship clearer. These prop-shape choices and examples are covered in React’s props guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep props read-only

Treat props as inputs to a render, not as data for a child to modify. If an interaction should change displayed data, have the child call a function supplied by the parent; the parent updates its own data and renders the child again with new props. This keeps ownership of the value with the component that can update it.

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

Know the client/server boundary

The examples here describe ordinary client-side component composition. In a server-component setup, event handlers cannot be passed as props across a client/server boundary; the exact boundary rules depend on the framework and component context. React’s error reference for error 374 flags this limitation, so do not assume a callback prop can cross every such boundary.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.