mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
👍 react-use
A collection of essential React Hooks.
Setup
You need to have React 16.8.0 or later installed to use the Hooks API.
npm i react-use
Sensors
useBattery— track device battery stateuseGeolocation— track geo location state of user's deviceuseHoveranduseHoverDirty— track mouse hover state of some elementuseIdle— track whether user is being inactiveuseKey,useKeyPress,useKeyboardJs, anduseKeyPressEvent— track keyboard inputuseLocation— track window location stateuseMedia— track a CSS media queryuseMediaDevices— track state of connected hardware devicesuseMotion— track state of device's motion sensoruseMouseanduseMouseHovered— track state of mouse positionuseNetwork— track state of user's internet connectionuseOrientation— track state of device's screen orientationusePageLeave— trigger callback when mouse leaves page boundariesuseScroll— track an HTML element's scroll positionuseSize— track an HTML element's dimensionsuseWindowScroll— trackWindowscroll positionuseWindowSize— trackWindowdimensions
UI
useAudio— play audio, track its state and hook up controlsuseClickAway— trigger callback when user clicks outside target areauseCss— dynamically adjusts CSSuseDropanduseDropArea— track file, link and copy-paste dropsuseFullscreen— display an element or video full-screenuseSpeech— synthesize speech audio from a text stringuseVideo— play video, track its state and hook up playback controlsuseWait— complex waiting management for UIs
Animation
useRaf— re-render component on eachrequestAnimationFrameuseSpring— interpolate number over time according to spring dynamicsuseTimeout— returns true after a timeoutuseTween— re-renders component, while tweening a number from 0 to 1useUpdate— returns a callback, which re-renders component when called
Side-effects
useAsync— resolve anasyncfunctionuseAsyncRetry—useAsyncwith aretry()callbackuseDebounce— debounce a functionuseFavicon— set favicon of the pageuseLocalStorage— manage a value inlocalStorageuseLockBodyScroll— lock scrolling of the body elementuseSessionStorage— manage a value insessionStorageuseThrottleanduseThrottleFn— throttle a functionuseTitle— set title of the page
Lifecycle
useEffectOnce— a modifieduseEffecthook that runs aneffectonly onceuseEvent— subscribe to eventsuseLifecycles— callmountandunmountcallbacksuseRefMounted— track if component is mountedusePromise— resolve promise only while component is mounteduseLogger—console.logas component goes through lifecyclesuseMount— run aneffecton mountuseUnmount— run aneffecton unmountuseUpdateEffect— run aneffectonly on updates
State
createMemo— factory of memoized hooksuseGetSet— returns state getterget()instead of raw stateuseGetSetState— as ifuseGetSetanduseSetStatehad a babyuseObservable— track latest value of anObservable.useSetState— createssetStatemethod which works likethis.setStateuseToggleanduseBoolean— track state of a booleanuseCounteranduseNumber— track state of a numberuseList— track state of an arrayuseMap— track state of an object
Usage
You can import each hook individually
import useToggle from 'react-use/lib/useToggle'
or use ES6 named import
import {useToggle} from 'react-use'
Depending on your bundler you might run into a missing dependency error. 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
}
]
Translations
License
Unlicense — public domain.
Languages
TypeScript
99.6%
JavaScript
0.4%