Course contentsShow
← React
Lesson 998 of 2,39625. Custom HooksFree lesson

The Cost of Premature Extraction

Understand why extracting hooks too early creates unnecessary abstractions and makes code harder to understand and modify.

The Cost of Premature Extraction

You'll learn: Why pulling logic into custom hooks too early can hurt your codebase more than it helps.

After learning to identify repeated logic (step 997), you might feel eager to extract every piece of logic into a custom hook. Resist this urge.

The Premature Abstraction Problem

Creating a custom hook before you truly understand the patterns creates unnecessary abstraction. You're essentially guessing at what's reusable before you've seen the real variations across your app.

This causes three main problems:

1. Harder to Understand
Instead of seeing the logic directly in your component, readers must jump to another file, read the hook, then return. For simple logic, this context-switching costs more than it saves.

2. Harder to Modify
When one component needs slightly different behavior, you face a dilemma: add parameters to the hook (increasing complexity for all users), create a second similar hook (duplication), or inline the logic back (wasting the extraction effort).

3. False Abstraction
What looked like "the same logic" in two places often diverges over time. Product requirements change. That extracted hook becomes a straitjacket.

Wait for the Rule of Three

A good guideline: wait until you've written the same logic three times before extracting. By then, you understand the true pattern and genuine variations. Your hook will actually solve the right problem.

Think of it like organizing your closet when you've only owned five shirts — you don't yet know the real categories you need.

Key Takeaway: Premature extraction creates abstractions that obscure simple logic and resist necessary changes. Wait until patterns prove themselves across multiple real use cases before extracting custom hooks.