Collection of essential React Hooks. Port of
libreact.
    Translations: π¨π³ ζ±θ―
npm i react-use
- Sensors
- useBatteryβ tracks device battery state.
- useGeolocationβ tracks geo location state of user's device.
- useHoverand- useHoverDirtyβ tracks mouse hover state of some element.
- useHashβ tracks location hash value.
- useIdleβ tracks whether user is being inactive.
- useIntersectionβ tracks an HTML element's intersection.
- useKey,- useKeyPress,- useKeyboardJs, and- useKeyPressEventβ track keys.
- useLocationand- useSearchParamβ tracks page navigation bar location state.
- useLongPressβ tracks long press gesture of some element.
- useMediaβ tracks state of a CSS media query.
- useMediaDevicesβ tracks state of connected hardware devices.
- useMotionβ tracks state of device's motion sensor.
- useMouseand- useMouseHoveredβ tracks state of mouse position.
- useMouseWheelβ tracks deltaY of scrolled mouse wheel.
- useNetworkStateβ tracks the state of browser's network connection.
- useOrientationβ tracks state of device's screen orientation.
- usePageLeaveβ triggers when mouse leaves page boundaries.
- useScratchβ tracks mouse click-and-scrub state.
- useScrollβ tracks an HTML element's scroll position.
- useScrollingβ tracks whether HTML element is scrolling.
- useStartTypingβ detects when user starts typing.
- useWindowScrollβ tracks- Windowscroll position.
- useWindowSizeβ tracks- Windowdimensions.
- useMeasureand- useSizeβ tracks an HTML element's dimensions.
- createBreakpointβ tracks- innerWidth
- useScrollbarWidthβ detects browser's native scrollbars width.
- usePinchZoomβ tracks pointer events to detect pinch zoom in and out status.
 
 
- UI
- useAudioβ plays audio and exposes its controls.
- useClickAwayβ triggers callback when user clicks outside target area.
- useCssβ dynamically adjusts CSS.
- useDropand- useDropAreaβ tracks file, link and copy-paste drops.
- useFullscreenβ display an element or video full-screen.
- useSliderβ provides slide behavior over any HTML element.
- useSpeechβ synthesizes speech from a text string.
- useVibrateβ provide physical feedback using the Vibration API.
- useVideoβ plays video, tracks its state, and exposes playback controls.
 
 
- Animations
- useRafβ re-renders component on each- requestAnimationFrame.
- useIntervaland- useHarmonicIntervalFnβ re-renders component on a set interval using- setInterval.
- useSpringβ interpolates number over time according to spring dynamics.
- useTimeoutβ re-renders component after a timeout.
- useTimeoutFnβ calls given function after a timeout.
- useTweenβ re-renders component, while tweening a number from 0 to 1.
- useUpdateβ returns a callback, which re-renders component when called.
 
 
- Side-effects
- useAsync,- useAsyncFn, and- useAsyncRetryβ resolves an- asyncfunction.
- useBeforeUnloadβ shows browser alert when user try to reload or close the page.
- useCookieβ provides way to read, update and delete a cookie.
- useCopyToClipboardβ copies text to clipboard.
- useDebounceβ debounces a function.
- useErrorβ error dispatcher.
- useFaviconβ sets favicon of the page.
- useLocalStorageβ manages a value in- localStorage.
- useLockBodyScrollβ lock scrolling of the body element.
- useRafLoopβ calls given function inside the RAF loop.
- useSessionStorageβ manages a value in- sessionStorage.
- useThrottleand- useThrottleFnβ throttles a function.
- useTitleβ sets title of the page.
- usePermissionβ query permission status for browser APIs.
 
 
- Lifecycles
- useEffectOnceβ a modified- useEffecthook that only runs once.
- useEventβ subscribe to events.
- useLifecyclesβ calls- mountand- unmountcallbacks.
- useMountedStateand- useUnmountPromiseβ track if component is mounted.
- usePromiseβ resolves promise only while component is mounted.
- useLoggerβ logs in console as component goes through life-cycles.
- useMountβ calls- mountcallbacks.
- useUnmountβ calls- unmountcallbacks.
- useUpdateEffectβ run an- effectonly on updates.
- useIsomorphicLayoutEffectβ- useLayoutEffectthat that works on server.
- useDeepCompareEffect,- useShallowCompareEffect, and- useCustomCompareEffect
 
 
- State
- createMemoβ factory of memoized hooks.
- createReducerβ factory of reducer hooks with custom middleware.
- createReducerContextand- createStateContextβ factory of hooks for a sharing state between components.
- useDefaultβ returns the default value when state is- nullor- undefined.
- useGetSetβ returns state getter- get()instead of raw state.
- useGetSetStateβ as if- useGetSetand- useSetStatehad a baby.
- useLatestβ returns the latest state or props
- usePreviousβ returns the previous state or props.
- usePreviousDistinctβ like- usePreviousbut with a predicate to determine if- previousshould update.
- useObservableβ tracks latest value of an- Observable.
- useRafStateβ creates- setStatemethod which only updates after- requestAnimationFrame.
- useSetStateβ creates- setStatemethod which works like- this.setState.
- useStateListβ circularly iterates over an array.
- useToggleand- useBooleanβ tracks state of a boolean.
- useCounterand- useNumberβ tracks state of a number.
- useList- andβ tracks state of an array.- useUpsert
- useMapβ tracks state of an object.
- useSetβ tracks state of a Set.
- useQueueβ implements simple queue.
- useStateValidatorβ tracks state of an object.
- useStateWithHistoryβ stores previous state values and provides handles to travel through them.
- useMultiStateValidatorβ alike the- useStateValidator, but tracks multiple states at a time.
- useMediatedStateβ like the regular- useStatebut with mediation by custom function.
- useFirstMountStateβ check if current render is first.
- useRendersCountβ count component renders.
- createGlobalStateβ cross component shared state.
- useMethodsβ neat alternative to- useReducer.
 
 
- Miscellaneous
- useEnsuredForwardedRefand- ensuredForwardRefβ use a React.forwardedRef safely.
 
  Usage β how to import.
  
  Unlicense β public domain.
  
  Support β add yourself to backer list below.