Course contentsShow
← React
Lesson 1155 of 2,39630. Composition: Inversion of ControlFree lesson

Control Through Callbacks: The Simplest Inversion

Passing functions as props to let consumers decide what happens during component lifecycle events.

Control Through Callbacks: The Simplest Inversion

What you'll learn: How to give consumers control over your component's behavior by accepting callback functions as props.

The Core Idea

The simplest form of inversion of control is passing functions as props. Instead of hardcoding what happens when a user clicks a button or when data loads, you let the consumer decide by accepting a callback function. You're inverting control from "I decide what happens" to "you tell me what to do."

Think of it like ordering a custom pizza: instead of the restaurant deciding all the toppings (rigid control), they ask you what you want (inverted control through your callback instructions).

How It Works

Your component handles the "when" (timing, lifecycle events) while the consumer handles the "what" (the actual behavior):

// Without inversion: component decides everything
function Button() {
  return <button onClick={() => alert('Clicked!')}>Click</button>;
}

// With inversion: consumer decides what happens
function Button({ onClick }: { onClick: () => void }) {
  return <button onClick={onClick}>Click</button>;
}

// Consumer now controls the behavior
<Button onClick={() => console.log('My custom action!')} />

Why This Matters

This pattern makes components reusable across different contexts. The same Button component works whether you need to submit a form, open a modal, or trigger an API call—because you control what happens through the callback.

Common callback props you've seen: onClick, onChange, onSubmit, onLoad, onError. Each inverts control of that specific event to the consumer.

Key Takeaway: Callback props are the simplest inversion of control—your component orchestrates when something happens, while the consumer decides what happens through the function they pass in.