Collection of essential React Hooks. Port of
libreact. See demos.
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.useKeyPressβ tracks whether a keyboard keyβor set of keysβwas pressed.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.useNetworkβ tracks state of user's internet connection.useOrientationβ tracks state of device's screen orientation.useSizeβ tracks some HTML element's dimensions.useWindowSizeβ tracksWindowdimensions.
- UI
useAudioβ plays audio and exposes its controls.useOutsideClickβ triggers callback when user clicks outside target area.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.useCssβ dynamically adjusts CSS.useFaviconβ sets favicon of the page.useLocalStorageβ manages a value inlocalStorage.useSessionStorageβ manages a value insessionStorage.useTitleβ sets title of the page.useDebounceβ debounces a function.
- Lifecycles
useLifecyclesβ callsmountandunmountcallbacks.useRefMountedβ tracks if component is mounted.useLoggerβ logs in console as component goes through life-cycles.useMountβ callsmountcallbacks.useUnmountβ callsunmountcallbacks.
- State
createMemoβ factory of memoized hooks.useCallbagβ tracks latest value of a callbag.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.1 or later installed to use Hooks API.
You can import each hook individually import useToggle from 'react-use/lib/useToggle'.
Unlicense β public domain.