#hooks

67 snippets tagged with #hooks

typescriptintermediate

useFetch — Generic Data Fetching Hook

Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.

Best for: API data fetching

#hooks#fetch
typescriptbeginner

useDebounce — Debounced Value Hook

Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.

Best for: Search-as-you-type

#hooks#debounce
typescriptintermediate

useLocalStorage — Persistent State Hook

Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.

Best for: User preferences

#hooks#localstorage
typescriptbeginner

useClickOutside — Outside Click Detection

Detect clicks outside a referenced element to close dropdowns, modals, and popover menus.

Best for: Dropdown menus

#hooks#click-outside
typescriptbeginner

useMediaQuery — Responsive Breakpoint Hook

Subscribe to CSS media query changes and get a boolean flag for responsive rendering logic in React.

Best for: Responsive layouts

#hooks#responsive
typescriptintermediate

Infinite Scroll with Intersection Observer

Load more items as the user scrolls using IntersectionObserver. No external libraries required.

Best for: Feed pagination

#infinite-scroll#intersection-observer
typescriptintermediate

useIntersectionObserver Hook

Track element visibility with the Intersection Observer API for lazy loading and scroll animations.

Best for: Lazy loading images

#hooks#intersection-observer
typescriptbeginner

usePrevious Hook

Track the previous value of any state or prop using a ref-based hook for comparison logic.

Best for: Detecting value changes

#hooks#state
typescriptbeginner

useWindowSize Hook

Track browser window dimensions with debounced resize handling for responsive component logic.

Best for: Responsive layouts

#hooks#responsive
typescriptbeginner

useCopyToClipboard Hook

Copy text to clipboard with success state and automatic reset timer using the Clipboard API.

Best for: Copy code snippets

#hooks#clipboard
typescriptintermediate

useFormValidation Hook

Lightweight form validation hook with field-level errors, touched tracking, and submit handling.

Best for: Contact forms

#hooks#forms
typescriptintermediate

React Custom Hook for Data Fetching

A reusable useFetch hook with loading states, error handling, caching, and abort support.

Best for: Reusable data fetching across components

#react#hooks
typescriptintermediate

React Form Validation Hook

A type-safe form validation hook with field-level errors, dirty tracking, and submit handling.

Best for: Type-safe form handling without libraries

#react#forms
typescriptintermediate

React Intersection Observer Hook

Custom useIntersectionObserver hook for lazy loading, infinite scroll, and scroll animations.

Best for: Lazy loading components when they enter viewport

#react#hooks
typescriptbeginner

React Responsive Breakpoint Hook

A useMediaQuery hook for responsive rendering with SSR-safe breakpoint detection.

Best for: Conditional rendering based on screen size

#react#responsive
typescriptintermediate

useThrottle Hook for Rate Limiting

Throttle rapidly-firing values like scroll or resize events with a configurable delay hook.

Best for: Scroll position tracking

#react#hooks
typescriptintermediate

useAsync Hook for Promise Management

Manage async operations with loading, error, and data states using a reusable hook pattern.

Best for: Data fetching with status tracking

#react#hooks
typescriptbeginner

useKeyboardShortcut Hook

Register global keyboard shortcuts with modifier keys, preventing defaults, and cleanup on unmount.

Best for: Command palette shortcuts

#react#hooks
typescriptintermediate

useMutation Hook for Side Effects

Handle mutations (POST/PUT/DELETE) with loading, error, reset, and optimistic update support.

Best for: Form submission handling

#react#hooks
typescriptadvanced

useInfiniteQuery Hook

Implement infinite scrolling with cursor-based pagination, loading states, and intersection observer.

Best for: Infinite scroll feeds

#react#hooks
typescriptbeginner

useEventListener Hook

Safely add and clean up DOM event listeners with a type-safe hook supporting window, document, and elements.

Best for: Window resize handling

#react#hooks
typescriptintermediate

useUndoRedo State History Hook

Add undo/redo capability to any state with history tracking, max size limits, and keyboard shortcuts.

Best for: Text editor undo/redo

#react#hooks
typescriptintermediate

useInterval Hook

Declarative setInterval hook that handles cleanup and dynamic delay changes safely.

Best for: Polling APIs

#hooks#timer
typescriptbeginner

useTimeout Hook

Declarative setTimeout hook with automatic cleanup and reset capabilities.

Best for: Auto-dismiss notifications

#hooks#timer
typescriptbeginner

useToggle Hook

Simple boolean toggle hook with set, toggle, on, and off functions.

Best for: Modal open/close state

#hooks#state
typescriptbeginner

useHover Hook

Tracks whether an element is being hovered using mouse enter/leave events with a ref.

Best for: Tooltip triggers

#hooks#mouse
typescriptbeginner

useOnlineStatus Hook

Tracks browser online/offline status and re-renders when connectivity changes.

Best for: Offline-first apps

#hooks#network
typescriptintermediate

useScrollPosition Hook

Tracks window scroll position with throttling for performance-sensitive scroll effects.

Best for: Sticky headers

#hooks#scroll
typescriptbeginner

useLockBodyScroll Hook

Locks body scroll when active, commonly used with modals and overlays to prevent background scrolling.

Best for: Modal dialogs

#hooks#scroll
typescriptbeginner

useDocumentTitle Hook

Dynamically updates the document title and restores it on unmount.

Best for: Page title updates

#hooks#document
typescriptadvanced

useFocusTrap Hook

Traps keyboard focus within a container for accessible modals and dialogs.

Best for: Accessible modals

#hooks#a11y
typescriptintermediate

useIdleDetection Hook

Detects user inactivity after a configurable timeout by monitoring mouse, keyboard, and touch events.

Best for: Auto-logout

#hooks#idle
typescriptintermediate

useStepWizard Hook

Multi-step wizard hook that manages step navigation, validation gates, and completion state.

Best for: Onboarding flows

#hooks#wizard
typescriptbeginner

useClipboard Hook (Advanced)

Clipboard hook with read, write, and auto-reset of the copied state after a timeout.

Best for: Copy-to-clipboard buttons

#hooks#clipboard
typescriptadvanced

useFetch with Cache Hook

Data fetching hook with built-in cache, loading, error states, and automatic revalidation.

Best for: API data fetching

#hooks#fetch
typescriptintermediate

useDarkMode Hook

Dark mode hook that syncs with system preferences and persists user choice to localStorage.

Best for: Theme switching

#hooks#dark-mode
typescriptintermediate

usePermission Hook

Queries and tracks browser permission state for features like notifications, camera, and geolocation.

Best for: Permission-gated features

#hooks#permissions
typescriptadvanced

useMutationObserver Hook

Wraps the MutationObserver API in a React hook to observe DOM changes on a target element.

Best for: Third-party widget monitoring

#hooks#dom
typescriptintermediate

useCountdown Hook

Countdown timer hook with start, pause, reset, and formatted time remaining output.

Best for: OTP expiry timers

#hooks#countdown
typescriptintermediate

useResizeObserver Hook

Tracks element dimensions using ResizeObserver with debounced updates.

Best for: Responsive layouts

#hooks#resize
typescriptintermediate

useGeolocation Hook

Accesses browser geolocation API with loading, error states, and watch mode support.

Best for: Location-based features

#hooks#geolocation
typescriptintermediate

useLongPress Hook

Detects long press gestures on touch and mouse devices with configurable threshold.

Best for: Context menus

#hooks#gesture
typescriptintermediate

useSearchParamsState Hook

Syncs component state with URL search parameters for shareable filter/sort states.

Best for: Filter state persistence

#hooks#url
typescriptadvanced

useWebSocket Hook

WebSocket hook with auto-reconnect, message queuing, and connection state tracking.

Best for: Chat applications

#hooks#websocket
typescriptintermediate

Infinite Scroll Hook v2

Intersection Observer-based infinite scroll with loading, error, and hasMore states.

Best for: Feed-style content

#hooks#infinite-scroll
typescriptadvanced

useHistoryState Hook

State hook that maintains full history with undo, redo, and time-travel capabilities.

Best for: Drawing apps

#hooks#undo
typescriptadvanced

useMediaRecorder Hook

Wraps the MediaRecorder API for audio/video recording with start, stop, and blob output.

Best for: Voice memos

#hooks#media
typescriptadvanced

useSelectionRange Hook

Tracks user text selection within a container, returning selected text and range coordinates.

Best for: Text annotation tools

#hooks#selection
typescriptadvanced

useIndexedDB Hook

Simple IndexedDB wrapper hook for persistent client-side storage with get, set, and delete.

Best for: Offline data storage

#hooks#indexeddb
typescriptadvanced

useTransitionGroup Hook

Manages enter/exit animations for list items with staggered transitions.

Best for: Animated list additions/removals

#hooks#animation
typescriptintermediate

useSpeechSynthesis Hook

Text-to-speech hook using the Web Speech API with voice selection, rate, and pitch control.

Best for: Accessibility features

#hooks#speech
typescriptintermediate

useNetworkSpeed Hook

Monitors the Network Information API to report connection type, downlink speed, and effective type.

Best for: Adaptive media quality

#hooks#network
typescriptintermediate

usePrefetch Hook

Prefetches data or resources when the user hovers over an element to reduce perceived latency.

Best for: Route prefetching

#hooks#prefetch
typescriptbeginner

useAbortController Hook

Creates and manages AbortController instances for cancelling fetch requests on unmount.

Best for: Cancelling stale requests

#hooks#abort
typescriptbeginner

usePageVisibility Hook

Detects when the page is hidden or visible using the Page Visibility API.

Best for: Pausing animations

#hooks#visibility
typescriptbeginner

useDebouncedCallback Hook

Returns a debounced version of a callback that delays execution until the pause in calls.

Best for: Search input debouncing

#hooks#debounce
typescriptintermediate

useScreenWakeLock Hook

Prevents the screen from turning off using the Screen Wake Lock API.

Best for: Video playback

#hooks#wake-lock
typescriptbeginner

useBreakpoints Hook

Returns the current Tailwind-style breakpoint based on window width for responsive logic.

Best for: Responsive component logic

#hooks#responsive
typescriptintermediate

useThrottledValue Hook

Throttles a rapidly changing value to update at most once per specified interval.

Best for: Scroll position tracking

#hooks#throttle
typescriptadvanced

useStorageEvent Hook

Syncs state across browser tabs by listening to localStorage storage events.

Best for: Theme sync across tabs

#hooks#storage
typescriptbeginner

useFormDirty Hook

Tracks whether a form has unsaved changes and warns before page unload.

Best for: Form unsaved-changes warning

#hooks#form
typescriptintermediate

useFullscreen Hook

Toggle fullscreen mode on an element using the Fullscreen API with state tracking.

Best for: Video players

#hooks#fullscreen
typescriptbeginner

useClickAway Hook

Fires a callback when a click is detected outside the referenced element.

Best for: Dropdown close on outside click

#hooks#click-outside
typescriptbeginner

usePersistentState Hook

Like useState but persists to localStorage with JSON serialization and SSR safety.

Best for: Persisting user preferences

#hooks#localstorage
typescriptbeginner

useIsomorphicLayoutEffect Hook

Safely uses useLayoutEffect on client and useEffect on server to avoid SSR warnings.

Best for: DOM measurement before paint

#hooks#ssr
bashintermediate

Git Hooks Pre-Commit Example

Git pre-commit and commit-msg hooks to enforce linting, tests, and commit message conventions.

Best for: Enforcing code quality before commits

#git#hooks
bashintermediate

Git Hooks — Pre-Commit Lint and Format

Set up pre-commit hooks to automatically lint, format, and validate code before every commit.

Best for: Enforcing code quality before commits

#git#hooks