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

Choosing the Right Pattern for Your API

Decision framework: when to use hooks for pure logic sharing vs render props for rendering control.

Choosing the Right Pattern for Your API

What you'll learn: How to decide whether hooks or render props best serve your component API based on what users need to control.

When designing a reusable component or library, you need to choose: should you expose logic via hooks, or should you use render props? The answer depends on what you're sharing.

The Decision Framework

Use Hooks when:

  • You're sharing pure logic (state, effects, calculations)
  • Users don't need to control rendering
  • The logic can stand alone without JSX
  • You want the cleanest, most composable API

Think of hooks like handing someone ingredients and a recipe. They can cook however they want.

// Hook: user controls all rendering
const { position } = useMousePosition();
return <div>X: {position.x}</div>; // User decides layout

Use Render Props when:

  • You're providing both logic AND rendering structure
  • Users need to customize rendering within your component tree
  • You need to control rendering order or wrapping elements
  • The component owns DOM structure that users customize

Think of render props like a restaurant where you choose the filling, but they build the sandwich their way.

// Render prop: component controls wrapper, user customizes content
<MouseTracker>
  {(position) => <div>X: {position.x}</div>}
</MouseTracker>

The Practical Test

Ask: "Does the user need to inject rendering into my component's structure?" If yes, render props. If they just need the data or behavior to use freely, hooks win.

Modern best practice: Hooks first, render props when rendering control matters. You can even offer both—a hook for power users, a render-prop wrapper for convenience.

Key Takeaway: Use hooks for sharing logic that users render freely; use render props when users need to customize rendering within your component's structure. When in doubt, start with a hook—it's simpler and more flexible.