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

The Three-Strike Rule for Hook Extraction

Apply the practical guideline: copy logic twice, extract on the third use to balance abstraction cost with reuse benefits.

The Three-Strike Rule for Hook Extraction

You'll learn a practical guideline for deciding when to extract custom hooks: copy logic twice, extract on the third use.

The Rule Explained

When you notice similar logic appearing in your components, your instinct might be to immediately extract it into a custom hook. But premature abstraction has costs: extra indirection, harder debugging, and time spent designing an API that might not fit future needs.

The Three-Strike Rule offers balance: let logic repeat twice, extract on the third occurrence.

Why This Works

Strike One: You write logic in a component. It works. No action needed.

Strike Two: You need similar logic elsewhere. You copy-paste it, maybe with slight modifications. This duplication reveals what varies and what stays consistent—crucial information for designing a good abstraction.

Strike Three: A third component needs it. Now you have enough examples to see the true pattern. Extract the custom hook with confidence, knowing its API will actually fit your real use cases.

The Real-World Analogy

Think of it like hiring an assistant. You wouldn't hire someone after doing a task once—you don't know if it'll recur. After twice, you understand the task better. By the third time, you know exactly what instructions the assistant needs.

When to Break the Rule

If logic is complex and error-prone (authentication, data synchronization), extract it immediately. If it's trivial (a single useState call), maybe never extract it. The Three-Strike Rule is for the vast middle ground: logic substantial enough to benefit from extraction but not so critical that duplication risks errors.

Key Takeaway: Copy logic once (two instances total), extract into a custom hook on the third use—this balances the cost of abstraction against the benefit of reuse and gives you real examples to design a better API.