Skip to Content
vitus·labs
Hooks

Hooks

36 React hooks for UI state, DOM interactions, events, timing, theming, accessibility, and SSR.

A collection of 36 React hooks covering UI state management, persistence, DOM interactions, event handling, timing, theming, accessibility, and SSR.

What's new in 2.9 — useMediaQuery, useBreakpoint and useLocalStorage (and therefore useColorScheme / useReducedMotion) are now hydration-safe. They are built on useSyncExternalStore with a server snapshot (false, the smallest breakpoint, and initialValue respectively): the server and the hydrating client render that value, then React re-renders with the real one right after hydration. Client-only renders still get the real value on the first render. Also: useBreakpoint shares one rAF-throttled resize listener across all instances, useLocalStorage keeps same-key instances in sync within the tab, useFocusTrap no longer lets focus escape, and useEventListener no longer re-attaches when options is an inline object.

React Native — the native entry point exports 21 of the 36 hooks. DOM-only hooks (useClickOutside, useCopyToClipboard, useElementSize, useEventListener, useFocusTrap, useIntersection, useLocalStorage, useMediaQuery, useOnlineStatus, usePageVisibility, useResizeObserver, useScrollLock, useWindowScroll) and the web-only useFocus / useHover are not available there. useBreakpoint, useColorScheme, useReducedMotion and useWindowResize use RN APIs (Dimensions, Appearance, AccessibilityInfo); useKeyboard is a no-op.

Installation

npm install @vitus-labs/hooks

Try It Live

Hook Categories

State Management

HookDescription
useControllableStateUnified controlled/uncontrolled pattern
useToggleBoolean toggle with convenience methods
usePreviousPrevious render value
useLatestAlways-current value ref
useLocalStorageState synced with localStorage (hydration-safe, cross-tab)

DOM & Measurement

HookDescription
useElementSizeTrack element dimensions via ResizeObserver
useResizeObserverFull contentRect of a ref'd element
useIntersectionObserve element visibility
useClickOutsideDetect clicks outside an element
useMergedRefCombine multiple refs into one
useScrollLockLock page scroll
useWindowResizeTrack viewport dimensions
useWindowScrollTrack window scroll position

Events & Interaction

HookDescription
useHoverMouse hover state
useFocusFocus/blur state
useFocusTrapTrap keyboard focus in container
useKeyboardListen for key presses
useEventListenerTyped DOM event subscription with cleanup
useCopyToClipboardCopy text with a transient "copied" flag

Timing

HookDescription
useDebouncedCallbackDebounce function calls
useDebouncedValueDebounce a value
useThrottledCallbackThrottle function calls
useIntervalDeclarative setInterval
useTimeoutDeclarative setTimeout

Theme & Responsive

HookDescription
useBreakpointActive breakpoint name
useMediaQueryCSS media query matching
useThemeValueDeep-read theme values
useRootSizeRoot font size + px/rem conversion
useSpacingSpacing scale function

Accessibility, SSR & Environment

HookDescription
useColorSchemeOS color scheme preference
useReducedMotionMotion preference
useIsClientfalse on server / first render, true after mount
usePageVisibilityWhether the tab is visible
useOnlineStatusWhether the browser is online
useIsomorphicLayoutEffectSSR-safe layout effect
useUpdateEffectSkip-initial-mount effect
Edit on GitHub

Last updated on

On this page