@react-hookz/web

React hooks done right, for browser and SSR.

@react-hookz/web downloads @react-hookz/web version @react-hookz/web license

@react-hookz/webSimilar Packages:
Npm Package Weekly Downloads Trend
3 Years
🌟 Show real-time usage chart on @react-hookz/web's README.md, just copy the code below.
## Usage Trend
[![Usage Trend of @react-hookz/web](https://npm-compare.com/img/npm-trend/THREE_YEARS/@react-hookz/web.png)](https://npm-compare.com/@react-hookz/web#timeRange=THREE_YEARS)
Cumulative GitHub Star Trend
🌟 Show GitHub stars trend chart on @react-hookz/web's README.md, just copy the code below.
## GitHub Stars Trend
[![GitHub Stars Trend of @react-hookz/web](https://npm-compare.com/img/github-trend/@react-hookz/web.png)](https://npm-compare.com/@react-hookz/web)
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@react-hookz/web224,2262,164146 kB2215 days agoMIT
README for @react-hookz/web

@react-hookz/web

NPM Downloads Dependents Build Coverage

Γ— DISCORD Γ— RELEASES Γ—


@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/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 don't either.

Usage

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_modules fully 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.

Migrating from react-use

@react-hookz/web was built as a spiritual successor of react-use by one of its former maintainers.

Hooks list

  • Callback

    • useDebouncedCallback β€” Makes passed function debounced, otherwise acts like useCallback.
    • useRafCallback β€” Makes passed function to be called within next animation frame.
    • useThrottledCallback β€” Makes passed function throttled, otherwise acts like useCallback.
  • Lifecycle

    • useConditionalEffect β€” Like useEffect but callback invoked only if given conditions match a given predicate.
    • useCustomCompareEffect β€” Like useEffect but uses a provided comparator function to validate dependency changes.
    • useDebouncedEffect β€” Like useEffect, but passed function is debounced.
    • useDeepCompareEffect β€” Like useEffect but uses @react-hookz/deep-equal comparator function to validate deep dependency changes.
    • useFirstMountState β€” Returns a boolean that is true only on first render.
    • useIntervalEffect β€” Like setInterval but in the form of a React hook.
    • useIsMounted β€” Returns a function that yields current mount state.
    • useIsomorphicLayoutEffect β€” Like useLayoutEffect but falls back to useEffect during SSR.
    • useMountEffect β€” Run an effect only when a component mounts.
    • useRafEffect β€” Like useEffect, but the effect is only run within an animation frame.
    • useRerender β€” Returns a callback that re-renders the component.
    • useThrottledEffect β€” Like useEffect, but the passed function is throttled.
    • useTimeoutEffect β€” Like setTimeout, 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 β€” Like useState, but its state setter accepts an extra argument, that allows cancelling renders.
    • useCounter β€” Tracks a numeric value and offers functions for manipulating it.
    • useDebouncedState β€” Like useState but its state setter is debounced.
    • useFunctionalState β€” Like useState but 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 a Map.
    • useMediatedState β€” Like useState, 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 β€” Like React.useState, but state is only updated within animation frame.
    • useRenderCount β€” Tracks component's render count including first render.
    • useSet β€” Tracks the state of a Set.
    • useToggle β€” Like useState, but can only be true or false.
    • useThrottledState β€” Like useState but 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 β€” Like useRef, but it returns an immutable ref that contains the actual value.
    • useCustomCompareMemo β€” Like useMemo but uses provided comparator function to validate dependency changes.
    • useDeepCompareMemo β€” Like useMemo but uses @react-hookz/deep-equal comparator function to validate deep dependency changes.
    • useHookableRef β€” Like useRef but it is possible to define handlers for getting and setting the value.
  • Side-effect

  • Sensor

    • useIntersectionObserver β€” Observe changes in the intersection of a target element with an ancestor element or with the viewport.
    • useMeasure β€” Uses ResizeObserver to 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 whenever ResizeObserver detects a change to the target's size.
    • useScreenOrientation β€” Checks if the screen is in portrait or landscape orientation and automatically re-renders on orientation change.
    • useDocumentVisibility β€” Tracks document visibility state.
  • 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.

Contributors

xobotyi
Anton Zinovyev
JoeDuncko
Joe Duncko
ArttuOll
Arttu Olli
kylemh
Kyle Holmberg
wesgro
Jake Ketcheson
Rey-Wang
Rey Wang
AndreasNel
Andreas Nel
fengkx
Fengkx
paul-sachs
Paul Sachs
Myzel394
Null
michaltarasiuk
Null
KonradLinkowski
Konrad Linkowski
jpwallace22
Justin Wallace
JoshuaStewartEntelect
Joshua Stewart
dantman
Daniel Friesen
ChloeMouret
Null
punkle
Brian Fletcher
birant
Birant Δ°yigΓΌn
lensbart
Bart Lens
axelboc
Axel Bocciarelli
akd-io
Anders Kjær Damgaard