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.useHoveranduseHoverDirtyβ tracks mouse hover state of some element.useIdleβ tracks whether user is being inactive.useKey,useKeyPress,useKeyboardJs, anduseKeyPressEventβ track keys.useLocationβ tracks page navigation bar location state.useMediaβ tracks state of a CSS media query.useMediaDevicesβ tracks state of connected hardware devices.useMotionβ tracks state of device's motion sensor.useMouseanduseMouseHoveredβ tracks state of mouse position.useNetworkβ tracks state of user's internet connection.useOrientationβ tracks state of device's screen orientation.usePageLeaveβ triggers when mouse leaves page boundaries.useScrollβ tracks an HTML element's scroll position.useSizeβ tracks an HTML element's dimensions.useStartTypingβ detects when user starts typing.useWindowScrollβ tracksWindowscroll position.useWindowSizeβ tracksWindowdimensions.
- UI
useAudioβ plays audio and exposes its controls.useClickAwayβ triggers callback when user clicks outside target area.useCssβ dynamically adjusts CSS.useDropanduseDropAreaβ tracks file, link and copy-paste drops.useFullscreenβ display an element or video full-screen.useSpeechβ synthesizes speech from a text string.useVideoβ plays video, tracks its state, and exposes playback controls.useWaitβ complex waiting management for UIs.
- Animations
useRafβ re-renders component on eachrequestAnimationFrame.useSpringβ interpolates number over time according to spring dynamics.useTimeoutβ returns true 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β resolves anasyncfunction.useAsyncFnβ state management for an async functionuseAsyncRetryβuseAsyncwithretry()method.useBeforeUnloadβ shows browser alert when user try to reload or close the page.useCopyToClipboardβ copies text to clipboard.useDebounceβ debounces a function.useFaviconβ sets favicon of the page.useLocalStorageβ manages a value inlocalStorage.useLockBodyScrollβ lock scrolling of the body element.useSessionStorageβ manages a value insessionStorage.useThrottleanduseThrottleFnβ throttles a function.useTitleβ sets title of the page.
- Lifecycles
useEffectOnceβ a modifieduseEffecthook that only runs once.useEventβ subscribe to events.useLifecyclesβ callsmountandunmountcallbacks.useRefMountedβ tracks if component is mounted.usePromiseβ resolves promise only while component is mounted.useLoggerβ logs in console as component goes through life-cycles.useMountβ callsmountcallbacks.useUnmountβ callsunmountcallbacks.useUpdateEffectβ run aneffectonly on updates.useDeepCompareEffectβ run aneffectdepending on deep comparison of its dependencies
- State
createMemoβ factory of memoized hooks.useGetSetβ returns state getterget()instead of raw state.useGetSetStateβ as ifuseGetSetanduseSetStatehad a baby.useObservableβ tracks latest value of anObservable.useSetStateβ createssetStatemethod which works likethis.setState.useToggleanduseBooleanβ tracks state of a boolean.useCounteranduseNumberβ tracks state of a number.useListβ tracks state of an array.useMapβ tracks state of an object.
You need to have React 16.8.0 or later installed to use the Hooks API. You can import each hook individually
import useToggle from 'react-use/lib/useToggle'or use ES6 named imports
import {useToggle} from 'react-use'Depending on your bundler you might run into a missing dependency error with ES6 named import statements. Some hooks require you to install peer dependencies so we recommend using individual imports. If you want the best of both worlds you can transform the named import statements to individual import statements with babel-plugin-import by adding the following config to your .babelrc file:
[
"import", {
"libraryName": "react-use",
"libraryDirectory": "lib",
"camel2DashComponentName": false
}
]Unlicense β public domain.