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

The Cohesion Test: Does the Hook Have a Clear Purpose?

Evaluate whether extracted logic forms a cohesive unit with a single responsibility or is just grouped arbitrary state.

The Cohesion Test: Does the Hook Have a Clear Purpose?

You'll learn how to evaluate whether your custom hook has a single, clear responsibility or is just a pile of loosely related state.

What is Cohesion?

Cohesion measures how well the pieces inside something belong together. A highly cohesive custom hook does one thing well. All its internal state, effects, and logic work toward a single, describable purpose. Low cohesion means you've bundled unrelated concerns together just because they happened to appear in the same component.

The Test

Ask yourself: "Can I describe what this hook does in one clear sentence without using the word 'and'?"

Good examples:

  • useToggle - "Manages a boolean state with toggle functionality"
  • useLocalStorage - "Syncs a state value with localStorage"
  • useDebounce - "Returns a debounced version of a value"

Bad examples:

  • useFormStuff - "Manages form values and modal visibility and API loading state"
  • useDashboard - "Handles user data and notifications and theme preference"

These fail the cohesion test because they're just arbitrary collections of state that happened to be near each other.

Why It Matters

High cohesion makes hooks:

  • Reusable - Clear purpose means clear use cases
  • Testable - Single responsibility is easier to verify
  • Maintainable - Changes have predictable scope
  • Understandable - Others (including future you) know what it does

Low cohesion creates maintenance nightmares where changing one thing breaks seemingly unrelated features.

Key Takeaway: A custom hook should have a single, clearly describable purpose. If you can't explain what it does without listing multiple unrelated concerns, it fails the cohesion test and should be split into separate hooks.