mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
This change removes useCallbag hook, it also reverts back changes for
hooks that only have tiny dependencies or dependencies that could have
been part of the hook itself. Effectivly only KeyboardJS and Rebound are
large packages that only partially are used by react-use, hence require
user to install them separately.
BREAKING CHANGE: 🧨 useCallbag hook is removed, some hooks KeyboardJS and Rebound installed
separately
7.0 KiB
7.0 KiB
👍
react-use
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.useKeyPressEvent— callonKeyDownandonKeyUpcallbacks, when key 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.useScroll— some HTML element's scroll position.useSize— tracks some HTML element's dimensions.useWindowScroll— tracksWindowscroll position.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.useAsyncRetry—useAsyncwithretry()method.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.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.
- 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.
Usage
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'.
License
Unlicense — public domain.