React Custom Hooks
Centralized repository of reusable React hooks covering state management, side effects, browser APIs, and user interaction. Written in TypeScript with complete documentation.
Technologies Used
Links
Project Description
Every React project I work on ends up needing the same utilities: debouncing, local storage sync, click outside detection, media queries, and more. Instead of copying code between projects or installing multiple small packages, I created a centralized repository of hooks I can reuse everywhere.
This library is my personal toolkit that I’ve refined through multiple projects, ensuring each hook is well-tested, properly typed, and follows React best practices.
Features
- Over 20 hooks covering common use cases
- Zero dependencies - only React as peer dependency
- Full TypeScript support with proper type inference
- Tree-shakeable - only import what you need
- SSR compatible - safe for Next.js and other SSR frameworks
- Fully tested with complete test coverage
Installation
npm install @qazuor/react-hooks# orpnpm add @qazuor/react-hooks# oryarn add @qazuor/react-hooksAvailable Hooks
State Management
- useBoolean - Boolean state with
setTrue,setFalse,togglemethods - useToggle - Toggleable state with optional persistence
- useQueue - FIFO queue with
enqueue,dequeueoperations
Side Effects
- useTimeout - Delayed callback execution with
start,stop,reset - useInterval - Recurring callbacks with pause/resume functionality
- useHandledInterval - Enhanced interval with random delay support
- useDebounce - Debouncing values with configurable delay
Browser APIs
- useLocalStorage - Persistent state synced with localStorage
- useSessionStorage - Session storage management
- useCopyToClipboard - Clipboard read/write utilities
- useMediaQuery - Reactive media query matching
- useNetworkState - Network connectivity detection
- useVisibilityChange - Document visibility monitoring
- useWindowWidth - Window dimensions tracking
User Interaction
- useClickOutside - Detect clicks outside an element
- useIdleness - Monitor user activity/inactivity
- usePageLeave - Detect when user leaves the page
- useLockBodyScroll - Prevent body scroll (modals, overlays)
- useMeasure - Element dimensions via ResizeObserver
Development
- useLogger - Development logging with lifecycle tracking
Usage Example
import { useLocalStorage, useDebounce, useClickOutside,} from '@qazuor/react-hooks';
function SearchDropdown() { const [query, setQuery] = useLocalStorage('search-query', ''); const debouncedQuery = useDebounce(query, 300); const dropdownRef = useClickOutside<HTMLDivElement>(() => { setIsOpen(false); });
useEffect(() => { if (debouncedQuery) { fetchResults(debouncedQuery); } }, [debouncedQuery]);
return ( <div ref={dropdownRef}> <input value={query} onChange={(e) => setQuery(e.target.value)} /> {/* Results dropdown */} </div> );}Technical Details
Requirements
- React 18.2+
- TypeScript 4.9+ (for TypeScript users)
Bundle Size
Each hook is independently importable, so you only include what you use in your bundle. The entire library is under 5KB gzipped.
Project Metrics
Key Highlights
- 20+ hooks covering common use cases
- Zero dependencies - only React as peer dependency
- Full TypeScript support with excellent type inference
- Tree-shakeable - only import what you use
- SSR safe - compatible with Next.js and others
- Bundle under 5KB gzipped for entire library
Challenges & Solutions
SSR compatibility - Hooks accessing window broke in SSR
typeof window !== 'undefined' checks with safe initial values
Memory leaks in intervals - Timers not cleaned up on unmount
Rigorous cleanup in useEffect return functions
Complex type inference - Generics for hooks with multiple overloads
Well-defined overload signatures plus type tests
Why This Stack?
Excellent type inference for hook consumers
Fast testing with great DX
Minimal bundle size, no version conflicts
Future Improvements
- usePrevious - Track previous value
- useAsync - Async operation state management
- useIntersectionObserver - Viewport visibility detection
- useGeolocation - Browser geolocation API
- useEventListener - Simplified event listener management