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

Basic Callback Pattern

Passing a function defined in the parent and calling it from a child component's event handler.

Basic Callback Pattern

What you'll learn: How to let a child component communicate back to its parent by calling a function passed down as a prop.

The Core Idea

You already know how to pass data down from parent to child using props. But what if the child needs to tell the parent something happened — like a button was clicked or a value changed?

The callback pattern solves this: the parent defines a function, passes it to the child as a prop, and the child calls that function when something happens.

How It Works

Think of it like giving your child a walkie-talkie. The parent says "Here, take this device (function). When you need me, press the button (call the function)."

Parent component:

function Parent() {
  function handleClick() {
    alert('Child button was clicked!');
  }

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

Child component:

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

When the user clicks the button, props.onButtonClick is called, which triggers the parent's handleClick function. The child doesn't know what the function does — it just calls it.

Why This Matters

This pattern lets components stay independent while still communicating. The child handles user interaction, the parent handles business logic. Neither needs to know the other's implementation details.

Key Takeaway: The callback pattern passes a parent function to a child as a prop, letting the child "call back" to the parent when events occur.