Package Exports
- @react-hookz/web
- @react-hookz/web/cjs/index.js
- @react-hookz/web/esm
- @react-hookz/web/esm/index.js
- @react-hookz/web/esm/useEventListener/useEventListener
- @react-hookz/web/esm/useEventListener/useEventListener.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@react-hookz/web) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@react-hookz/web is a library of general-purpose React hooks built with care and SSR compatibility
in mind.
Install
This one is pretty simple, everyone knows what to do:
npm i @react-hookz/web
# or
yarn add @react-hookz/webAs 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.
Usage
This package provides three levels of compilation:
- Main, the
/cjsfolder — CommonJS modules, with ES5 lang level. - ESM, the
/esmfolder — ES modules (browser compatible), with ES5 lang level. - ESNext, the
/esnextfolder — ES modules (browser compatible), with ESNext lang level.
So, if you need the useMountEffect hook, depending on your needs, you can import it 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';Migrating from react-use
@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.
Hooks list
Callback
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.
Lifecycle
useConditionalEffect— LikeuseEffectbut callback invoked only if given conditions match a given predicate.useCustomCompareEffect— LikeuseEffectbut uses a provided comparator function to validate dependency changes.useDebouncedEffect— LikeuseEffect, but passed function is debounced.useDeepCompareEffect— LikeuseEffectbut uses@react-hookz/deep-equalcomparator function to validate deep dependency changes.useFirstMountState— Returns a boolean that istrueonly on first render.useIntervalEffect— LikesetIntervalbut in the form of a React hook.useIsMounted— Returns a function that yields current mount state.useIsomorphicLayoutEffect— LikeuseLayoutEffectbut falls back touseEffectduring SSR.useMountEffect— Run an effect only when a component mounts.useRafEffect— LikeuseEffect, but the effect is only run within an animation frame.useRerender— Returns a callback that re-renders the component.useThrottledEffect— LikeuseEffect, but the passed function is throttled.useTimeoutEffect— LikesetTimeout, but in the form of a React hook.useUnmountEffect— Run an effect only when a component unmounts.useUpdateEffect— An effect hook that ignores the first render (not invoked on mount).useLifecycleLogger— This hook provides logging when the component mounts, updates and unmounts.
State
useControlledRerenderState— LikeuseState, but its state setter accepts an extra argument, that allows cancelling renders.useCounter— Tracks a numeric value and offers functions for manipulating it.useDebouncedState— LikeuseSafeStatebut its state setter is debounced.useFunctionalState— LikeuseStatebut instead of raw state, a state getter function is 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 renders.useRafState— LikeReact.useState, but state is only updated within animation frame.useRenderCount— Tracks component's render count including first render.useSafeState— LikeuseState, but its state setter is guarded against setting the state of an unmounted component.useSet— Tracks the state of aSet.useToggle— LikeuseState, but can only betrueorfalse.useThrottledState— LikeuseSafeStatebut its state setter is throttled.useValidator— Performs validation when any of the provided dependencies change.
Navigator
useNetworkState— Tracks the state of the browser's network connection.useVibrate— Provides vibration feedback using the Vibration API.usePermission— Tracks the state of a permission.
Miscellaneous
useSyncedRef— LikeuseRef, but it returns an immutable ref that contains the actual value.useCustomCompareMemo— LikeuseMemobut uses provided comparator function to validate dependency changes.useDeepCompareMemo— LikeuseMemobut uses@react-hookz/deep-equalcomparator function to validate deep dependency changes.useHookableRef— LikeuseRefbut it is possible to define handlers for getting and setting the value.
Side-effect
useAsync— Executes provided async function and tracks its results and errors.useAsyncAbortable— LikeuseAsync, but also providesAbortSignalas first function argument to the async function.useCookieValue— Manages a single cookie.useLocalStorageValue— Manages a single LocalStorage key.useSessionStorageValue— Manages a single SessionStorage key.
Sensor
useIntersectionObserver— Observe changes in the intersection of a target element with an ancestor element or with the viewport.useMeasure— UsesResizeObserverto track an element's dimensions and to re-render the component when they change.useMediaQuery— Tracks the state of a CSS media query.useResizeObserver— Invokes a callback wheneverResizeObserverdetects a change to the target's size.useScreenOrientation— Checks if the screen is inportraitorlandscapeorientation and automatically re-renders on orientation change.
Dom
useClickOutside— Triggers a callback when the user clicks outside a target element.useEventListener— Subscribes an event listener to a target element.useKeyboardEvent— Invokes a callback when a keyboard event occurs on the chosen target.useWindowSize— Tracks the inner dimensions of the browser window.
