@react-hookz/web
is a library of general-purpose React hooks built with care and SSR compatibility
in mind.
This one is pretty simple, everyone knows what to do:
npm i @react-hookz/web
# or
yarn add @react-hookz/web
As hooks was introduced to the world in React 16.8, @react-hookz/web
requires - you guessed it -
react
and react-dom
16.8+.
Also, as React does not support IE, @react-hookz/web
does not do so either. You'll have to
transpile your node-modules
in order to run in IE.
This package provides three levels of compilation:
- Main, the
/cjs
folder — CommonJS modules, with ES5 lang level. - ESM, the
/esm
folder — it is ES modules (browser compatible), with ES5 lang level. - ESNext, the
/esnext
folder — it is ES modules (browser compatible), with ESNext lang level.
So, if you need the useMountEffect
hook, depending on your needs, you can import in three ways
(there are actually more, but these are the three most common):
// in case you need cjs modules
import { useMountEffect } from '@react-hookz/web';
// in case you need esm modules
import { useMountEffect } from '@react-hookz/web/esm';
// in case you want all the recent ES features
import { useMountEffect } from '@react-hookz/web/esnext';
@react-hookz/web
was built as a spiritual successor of react-use
by one of its former maintainers.
Coming from react-use
? Check out our
migration guide.
-
useDebouncedCallback
— Makes passed function debounced, otherwise acts likeuseCallback
.useRafCallback
— Makes passed function to be called within next animation frame.useThrottledCallback
— Makes passed function throttled, otherwise acts likeuseCallback
.
-
useConditionalEffect
— LikeuseEffect
but callback invoked only if conditions match predicate.useCustomCompareEffect
— LikeuseEffect
but uses provided comparator function to validate dependency changes.useDebouncedEffect
— LikeuseEffect
, but passed function is debounced.useDeepCompareEffect
— LikeuseEffect
but uses@react-hookz/deep-equal
comparator function to validate deep dependency changes.useFirstMountState
— Return boolean that istrue
only on first render.useIntervalEffect
— LikesetInterval
but in form of react hook.useIsMounted
— Returns function that yields current mount state.useIsomorphicLayoutEffect
—useLayoutEffect
for browser with fallback touseEffect
for SSR.useMountEffect
— Run effect only when component first-mounted.useRafEffect
— LikeReact.useEffect
, but effect is only run within animation frame.useRerender
— Return callback that re-renders component.useThrottledEffect
— LikeuseEffect
, but passed function is throttled.useTimeoutEffect
— LikesetTimeout
, but in the form of a react hook.useUnmountEffect
— Run effect only when component unmounted.useUpdateEffect
— Effect hook that ignores the first render (not invoked on mount).useLifecycleLogger
— This hook provides a console log when the component mounts, updates and unmounts.
-
useControlledRerenderState
— LikeReact.useState
, but its state setter accepts extra argument, that allows to cancel rerender.useCounter
— Tracks a numeric value and offers functions for manipulating it.useDebouncedState
— LikeuseSafeState
but its state setter is debounced.useFunctionalState
— LikeuseState
but instead of raw state, state getter returned.useList
— Tracks a list and offers functions for manipulating it.useMap
— Tracks the state of aMap
.useMediatedState
— LikeuseState
, but every value set is passed through a mediator function.usePrevious
— Returns the value passed to the hook on previous render.usePreviousDistinct
— Returns the most recent distinct value passed to the hook on previous render.useRafState
— LikeReact.useState
, but state is only updated within animation frame.useSafeState
— LikeuseState
, but its state setter is guarded against sets on unmounted component.useSet
— Tracks the state of aSet
.useToggle
— LikeuseState
, but can only becometrue
orfalse
.useThrottledState
— LikeuseSafeState
but its state setter is throttled.useValidator
— Performs validation when any of provided dependencies has changed.
-
useNetworkState
— Tracks the state of browser's network connection.useVibrate
— Provides vibration feedback using the Vibration API.usePermission
— Tracks a permission state.
-
useSyncedRef
— LikeuseRef
, but it returns immutable ref that contains actual value.useCustomCompareMemo
— Like useMemo but uses provided comparator function to validate dependency changes.useDeepCompareMemo
— LikeuseMemo
but uses@react-hookz/deep-equal
comparator function to validate deep dependency changes.useHookableRef
— LikeuseRef
but it is possible to define get and set handlers.
-
useAsync
— Executes provided async function and tracks its result and error.useAsyncAbortable
— LikeuseAsync
, but also providesAbortSignal
as first function argument to async function.useCookieValue
— Manages a single cookie.useLocalStorageValue
— Manages a single LocalStorage key.useSessionStorageValue
— Manages a single SessionStorage key.
-
useIntersectionObserver
— Observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.useMeasure
— Uses ResizeObserver to track element dimensions and re-render component when they change.useMediaQuery
— Tracks the state of CSS media query.useResizeObserver
— Invokes a callback whenever ResizeObserver detects a change to target's size.useScreenOrientation
— Checks if screen is inportrait
orlandscape
orientation and automatically re-renders on orientation change.
-
useClickOutside
— Triggers callback when user clicks outside the target element.useEventListener
— Subscribes an event listener to the target, and automatically unsubscribes it on unmount.useKeyboardEvent
— Executes callback when keyboard event occurred on target.useWindowSize
— Tracks window inner dimensions.