@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/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 don't either.
This package distributed with ESNext language level and ES modules system. It means that depending on your browser target you might need to transpile it. Every major bundler provides a way to transpile
node_modulesfully or partially. Address your bundler documentation for more details.
You can import hooks two ways:
// from the root of package
import {useMountEffect} from '@react-hookz/web';
// or single hook directly
import {useMountEffect} from '@react-hookz/web/useMountEffect/index.js';In case your bundler supports tree-shaking (most of modern does) - both variants are equal and only necessary code will get into your bundle. Direct hook imports should be considered otherwise.
@react-hookz/web was built as a spiritual successor of
react-use by one of its former maintainers.
-
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β 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.
-
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β LikeuseStatebut 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.useQueueβ A state hook implementing FIFO queue.useRafStateβ LikeReact.useState, but state is only updated within animation frame.useRenderCountβ Tracks component's render count including first render.useSetβ Tracks the state of aSet.useToggleβ LikeuseState, but can only betrueorfalse.useThrottledStateβ LikeuseStatebut its state setter is throttled.useValidatorβ Performs validation when any of the provided dependencies change.
-
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.
-
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.
-
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.
-
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.useDocumentVisibilityβ Tracks document visibility state.
-
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.
