Course contentsShow
← React
Lesson 1048 of 2,39627. Composition: Render PropsFree lesson

Inline Functions vs Extracted Functions

You can pass inline arrow functions or extract the render logic into a separate function for readability and reusability.

Inline Functions vs Extracted Functions

You'll learn how to choose between writing render prop functions inline versus extracting them into separate named functions for better readability and reuse.

When using render props, you have two ways to provide the render function: write it directly where you need it (inline), or define it separately and pass it by name.

The Inline Approach

You can write the function directly in JSX:

<MouseTracker render={(x, y) => (
  <div>Mouse is at {x}, {y}</div>
)} />

This is quick and convenient for simple, one-off renders. Everything stays in one place, making it easy to see what's happening at a glance.

The Extracted Approach

For more complex logic or when you need the same render function in multiple places, extract it:

function renderMousePosition(x, y) {
  return <div>Mouse is at {x}, {y}</div>;
}

// Later, in your JSX:
<MouseTracker render={renderMousePosition} />

Think of it like recipes: inline functions are like cooking instructions written directly in your shopping list—fine for simple meals. Extracted functions are like having a recipe card you can reference and reuse whenever you need it.

When to Use Each

Use inline functions when:

  • The logic is short and simple (1-3 lines)
  • It's only used once
  • Keeping everything together improves readability

Use extracted functions when:

  • The logic is complex or lengthy
  • You need the same render logic in multiple places
  • You want to name the function for clarity
  • You're writing tests for the render logic

Key Takeaway: Inline render props are perfect for simple, one-off cases, while extracted functions improve readability and enable reuse for complex or repeated logic.