Module 5.3: Custom Hooks - The Secret Weapon

🎯 What You’ll Learn
By the end of this lesson, you’ll master:
- Custom Hook Mindset: Understand how to separate Business Logic from
UI Components. - DRY Principle (Don’t Repeat Yourself): Reuse complex logic across different
components. - Building Practical Hooks: Hands-on experience writing popular hooks like
useDocumentTitle,useWindowSize, anduseDebounce.
1. The Mindset: Separating Logic from UI
Before Custom Hooks, sharing stateful logic across components was challenging. Custom Hooks empower you to extract logic from your components for easier testing and reuse.

1.1. Custom Hook Rules

- Name starts with
use: For example,useUser,useAuth. This is a mandatory convention that enables React’s ESLintfunctions to check your hooks. - It’s a JavaScript
function: You can calluseState,useEffect, or other hooks inside it. - Operates independently: Each time a
componentuses a hook, it creates its own separatestateand effects (it doesn’t sharestatebetweencomponents that use the same hook).
2. Practical Labs
Lab 1: Image Widget (useWindowSize)
Build a widget that displays screen size information and dynamically changes an image based on screen dimensions (Mobile/Tablet/Desktop).
- Features: Display current width/height, corresponding device icon (Mobile/Tablet/PC), and change background color based on breakpoints.
- Technique: Write your own
useWindowSizehook and use it within acomponent.

💡 Tip:
- Define breakpoints: Mobile (< 640px), Desktop (>= 640px).
- Use
useWindowSizeto get thewidth.- Conditionally render:
width < 640 ? <MobileIcon /> : <DesktopIcon />.
Lab 2: Smart Search (useDebounce)
Build a smart search box that reduces the number of API requests.
- Features: Search input, display “simulated” results, or make actual API calls.
- Technique:
ImplementuseDebounceto delay processing search logic until the user stops typing.

💡 Tip:
useDebounce(value, delay)returns the debounced value.useEffectwill listen fordebouncedValueinstead of the originalvaluefrom the input.- Display “Searching…”
statewhenvaluediffers fromdebouncedValue.
Lab 3: Scroll Progress & To Top (useScroll)
Create a progress bar that shows reading progress and a “Back to Top” button that appears only when scrolling down.
- Features:
- A horizontal progress bar at the top of the screen that tracks scroll position.
- A “Back to Top” arrow button that appears when scrolled past 300px.
- Technique: A Custom Hook
useScrollreturns thescrollYposition and calculates the percentage of the page read.

💡 Tip:
useScrolllistens to the window’sscrollevent.- Calculate percentage:
(scrollTop / (scrollHeight - clientHeight)) * 100.- The “Back to Top” button uses
window.scrollTo({ top: 0, behavior: 'smooth' }).
3. Key Takeaways
- Custom Hooks are React’s most powerful tool for logic reuse.
- Separation of Concerns:
Components should primarily containUI; push complex logic into Hooks. Librarys: In practice, consider using optimized hooklibrarys likeusehooks-ts,react-use, orahooksbefore writing your own (unless for learning purposes).
4. References
Last updated on