Course contentsShow
← React
Lesson 233 of 2,3967. Props FundamentalsFree lesson

Passing Arguments Through Callbacks

How child components call parent callbacks with data, enabling them to send information up the tree.

Passing Arguments Through Callbacks

You'll learn how child components can send data back up to their parents by calling callback functions with arguments.

The Two-Way Communication Pattern

So far you've passed functions down as props to let children notify parents that something happened. But what if the child needs to tell the parent specific information about what happened—like which button was clicked, what value was entered, or which item was selected?

The solution: call the parent's function with arguments.

When the child component invokes the callback prop, it can pass data as arguments. The parent function receives those arguments and can use them to update state, make decisions, or trigger other actions.

How It Works

The parent defines a function that expects parameters:

function ParentComponent() {
  function handleItemClick(itemId) {
    console.log('Item clicked:', itemId);
  }

  return <ChildComponent onItemClick={handleItemClick} />;
}

The child calls that function with data:

function ChildComponent(props) {
  return (
    <button onClick={() => props.onItemClick(42)}>
      Click Item 42
    </button>
  );
}

Notice the arrow function wrapper: () => props.onItemClick(42). This creates a new function that, when called by the click event, invokes the parent's callback with the specific argument 42.

Think of It Like a Form

Imagine a child filling out a form and submitting it to a parent. The parent doesn't just need to know the form was submitted—they need to know what was written on it. Arguments are the data written on that form, traveling upward through the callback.

Key Takeaway: Child components send information up the component tree by calling parent callbacks with arguments, enabling true two-way communication within React's one-way data flow.