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

Why Pass Functions as Props

Understanding that callbacks let child components communicate upward, inverting data flow in React's one-way system.

Why Pass Functions as Props

What you'll learn: How passing functions as props allows child components to send information back up to their parents, enabling two-way communication within React's one-way data flow.

The One-Way Data Problem

You know props flow downward only: parents pass data to children. But what if a child component needs to tell its parent something happened? A button was clicked, a form was submitted, a selection was made?

React doesn't allow children to directly modify parent data (props are read-only!). So we need another mechanism.

Functions as Communication Channels

When you pass a function as a prop, you're giving the child component a way to "call back" to the parent. The function itself is defined in the parent, so when the child executes it, code runs in the parent's scope—where the parent can update its own data.

Think of it like giving someone your phone number. They can't change your contacts, but they can call you to tell you something important.

function Parent() {
  const handleButtonClick = () => {
    console.log("Child told me the button was clicked!");
  };

  return <Child onButtonClick={handleButtonClick} />;
}

function Child({ onButtonClick }) {
  return <button onClick={onButtonClick}>Click Me</button>;
}

The child doesn't know what onButtonClick does—it just calls it. The parent decides the behavior.

Inverting the Flow

This pattern inverts data flow. Props go down, but function calls (and any arguments passed to them) communicate upward. This is how React maintains one-way data flow while still enabling interactivity.

Key Takeaway: Passing functions as props lets children communicate events upward to parents, enabling two-way interaction within React's strictly one-way data architecture.