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.useHover
anduseHoverDirty
β tracks mouse hover state of some element.useIdle
β tracks whether user is being inactive.useIntersection
β tracks an HTML element's intersection.useKey
,useKeyPress
,useKeyboardJs
, anduseKeyPressEvent
β track keys.useLocation
anduseSearchParam
β 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.useMouse
anduseMouseHovered
β 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.useScrolling
β tracks whether HTML element is scrolling.useSize
β tracks an HTML element's size.useStartTyping
β detects when user starts typing.useWindowScroll
β tracksWindow
scroll position.useWindowSize
β tracksWindow
dimensions.useMeasure
β tracks an HTML element's dimensions using the Resize Observer API.createBreakpoint
β tracksinnerWidth
- UI
useAudio
β plays audio and exposes its controls.useClickAway
β triggers callback when user clicks outside target area.useCss
β dynamically adjusts CSS.useDrop
anduseDropArea
β 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.
- Animations
useRaf
β re-renders component on eachrequestAnimationFrame
.useInterval
anduseHarmonicIntervalFn
β re-renders component on a set interval usingsetInterval
.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
β resolves anasync
function.useAsyncFn
β state management for an async functionuseAsyncRetry
βuseAsync
withretry()
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.useRafLoop
β calls given function inside the RAF loop.useSessionStorage
β manages a value insessionStorage
.useThrottle
anduseThrottleFn
β throttles a function.useTitle
β sets title of the page.usePermission
β query permission status for browser APIs.
- Lifecycles
useEffectOnce
β a modifieduseEffect
hook that only runs once.useEvent
β subscribe to events.useLifecycles
β callsmount
andunmount
callbacks.useMountedState
β track if component is mounted.usePromise
β resolves promise only while component is mounted.useLogger
β logs in console as component goes through life-cycles.useMount
β callsmount
callbacks.useUnmount
β callsunmount
callbacks.useUpdateEffect
β run aneffect
only on updates.useIsomorphicLayoutEffect
βuseLayoutEffect
that does not show warning when server-side rendering.useDeepCompareEffect
β run aneffect
depending on deep comparison of its dependencies
- State
createMemo
β factory of memoized hooks.createReducer
β factory of reducer hooks with custom middleware.useDefault
β returns the default value when state isnull
orundefined
.useGetSet
β returns state getterget()
instead of raw state.useGetSetState
β as ifuseGetSet
anduseSetState
had a baby.usePrevious
β returns the previous state or props.usePreviousDistinct
β likeusePrevious
but with a predicate to determine ifprevious
should update.useObservable
β tracks latest value of anObservable
.useRafState
β createssetState
method which only updates afterrequestAnimationFrame
.useSetState
β createssetState
method which works likethis.setState
.useStateList
β circularly iterates over an array.useToggle
anduseBoolean
β tracks state of a boolean.useCounter
anduseNumber
β tracks state of a number.useList
anduseUpsert
β tracks state of an array.useMap
β tracks state of an object.useStateValidator
β tracks state of an object.useMultiStateValidator
β alike theuseStateValidator
, but tracks multiple states at a time.useMediatedState
β like the regularuseState
but with mediation by custom function.
- Miscellaneous
useEnsuredForwardedRef
andensuredForwardRef
β use a React.forwardedRef safely.
Usage β how to import.
Unlicense β public domain.
Support β add yourself to backer list below.