Extracting Logic to Hooks: Parameters & Returns
Extract repeated logic into a custom hook. Choose inputs (parameters) and a stable return shape (object/array) for easy reuse.
Why custom hooks?
Goal: Move repeated logic into a custom hook. Hooks bundle state, effects, and helpers behind a clean API.
- Pick inputs (parameters)
- Choose return shape
- Keep names starting with use
Identify extractable logic
Good candidates:
- Fetch + loading/error wiring
- Debounce/throttle inputs
- DOM events (e.g., escape key)
Keep the API tiny and focused.
All lessons in this course
- Extracting Logic to Hooks: Parameters & Returns
- Naming, Memoization & Effect Hygiene
- Testing Custom Hooks (Preview)