Hooks
Collection of custom React hooks for common functionality across the application.
This document provides an overview of custom React hooks used throughout the application for common functionality like clipboard operations, keyboard shortcuts, responsive design, and theme management.
Remember to add "use client"
directive at the top of files that
use these hooks, as they rely on browser APIs.
useClipboard
Utility
Hook for copying text to clipboard with status feedback
Implementation
Usage
useKeyPress
Keyboard
Hook for handling keyboard shortcuts and key press events
Implementation
Usage
useIsMobile
Responsive
Hook for detecting mobile viewport (below 768px)
Implementation
Usage
useOS
Platform
Implementation
Usage
useThemeToggle
Theme
Implementation
Usage
useUpgradeModal
UI
Hook for managing upgrade modal state with URL query parameters
Implementation
Usage
Key Features
React Integration
⚛️ Seamless integration with React's lifecycle and state management
Type Safety
🔄 Full TypeScript support with proper type definitions
Cross-Platform
📱 Works across different devices and environments
Composable
🧩 Can be combined with other hooks for complex functionality
Best Practices
When using these hooks, keep in mind the following best practices:
- Client-side Only: All hooks are client-side only, so use them in client components with
"use client"
directive - Error Boundaries: Use with appropriate error boundaries to handle unexpected issues
- SSR Considerations: Be careful with hooks that rely on browser APIs in SSR contexts
- React Hooks Rules: Follow React hooks rules and conventions (call hooks at the top level)
Additional Hooks
useLocalStorage
Hook for persisting state in localStorage with auto serialization/deserialization.
useMediaQuery
Hook for responding to media queries.
useDebounce
Hook for debouncing values (useful for search inputs).
Creating Custom Hooks
When creating custom hooks, follow these guidelines to ensure consistency and maintainability.
Guidelines
- Naming: Start with
use
prefix (e.g.,useCustomHook
) - Organization: Place hooks in the
hooks/
directory - Type Safety: Use TypeScript generics for flexible, type-safe hooks
- Documentation: Add JSDoc comments describing parameters and return values
- Client Directive: Add
"use client"
directive for hooks using browser APIs - Error Handling: Implement proper error handling and fallbacks
- Testing: Write unit tests for hooks using React Testing Library