25.1.1 • Published 7 months ago

@react-hookz/web v25.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@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`**](./src/useDebouncedCallback/index.ts)
    		— Makes passed function debounced, otherwise acts like `useCallback`.
    	- [**`useRafCallback`**](./src/useRafCallback/index.ts)
    		— Makes passed function to be called within next animation frame.
    	- [**`useThrottledCallback`**](./src/useThrottledCallback/index.ts)
    		— Makes passed function throttled, otherwise acts like `useCallback`.
  • Lifecycle

    	- [**`useConditionalEffect`**](./src/useConditionalEffect/index.ts)
    		— Like `useEffect` but callback invoked only if given conditions match a given predicate.
    	- [**`useCustomCompareEffect`**](./src/useCustomCompareEffect/index.ts)
    		— Like `useEffect` but uses a provided comparator function to validate dependency changes.
    	- [**`useDebouncedEffect`**](./src/useDebouncedEffect/index.ts)
    		— Like `useEffect`, but passed function is debounced.
    	- [**`useDeepCompareEffect`**](./src/useDeepCompareEffect/index.ts)
    		— Like `useEffect` but uses `@react-hookz/deep-equal` comparator function to validate deep
    		dependency changes.
    	- [**`useFirstMountState`**](./src/useFirstMountState/index.ts)
    		— Returns a boolean that is `true` only on first render.
    	- [**`useIntervalEffect`**](./src/useIntervalEffect/index.ts)
    		— Like `setInterval` but in the form of a React hook.
    	- [**`useIsMounted`**](./src/useIsMounted/index.ts)
    		— Returns a function that yields current mount state.
    	- [**`useIsomorphicLayoutEffect`**](./src/useIsomorphicLayoutEffect/index.ts)
    		— Like `useLayoutEffect` but falls back to `useEffect` during SSR.
    	- [**`useMountEffect`**](./src/useMountEffect/index.ts)
    		— Run an effect only when a component mounts.
    	- [**`useRafEffect`**](./src/useRafEffect/index.ts)
    		— Like `useEffect`, but the effect is only run within an animation frame.
    	- [**`useRerender`**](./src/useRerender/index.ts)
    		— Returns a callback that re-renders the component.
    	- [**`useThrottledEffect`**](./src/useThrottledEffect/index.ts)
    		— Like `useEffect`, but the passed function is throttled.
    	- [**`useTimeoutEffect`**](./src/useTimeoutEffect/index.ts)
    		— Like `setTimeout`, but in the form of a React hook.
    	- [**`useUnmountEffect`**](./src/useUnmountEffect/index.ts)
    		— Run an effect only when a component unmounts.
    	- [**`useUpdateEffect`**](./src/useUpdateEffect/index.ts)
    		— An effect hook that ignores the first render (not invoked on mount).
    	- [**`useLifecycleLogger`**](./src/useLifecycleLogger/index.ts)
    		— This hook provides logging when the component mounts, updates and unmounts.
  • State

    	- [**`useControlledRerenderState`**](./src/useControlledRerenderState/index.ts)
    		— Like `useState`, but its state setter accepts an extra argument, that allows cancelling
    		renders.
    	- [**`useCounter`**](./src/useCounter/index.ts)
    		— Tracks a numeric value and offers functions for manipulating it.
    	- [**`useDebouncedState`**](./src/useDebouncedState/index.ts)
    		— Like `useState` but its state setter is debounced.
    	- [**`useFunctionalState`**](./src/useFunctionalState/index.ts)
    		— Like `useState` but instead of raw state, a state getter function is returned.
    	- [**`useList`**](./src/useList/index.ts)
    		— Tracks a list and offers functions for manipulating it.
    	- [**`useMap`**](./src/useMap/index.ts) — Tracks the
    		state of a `Map`.
    	- [**`useMediatedState`**](./src/useMediatedState/index.ts)
    		— Like `useState`, but every value set is passed through a mediator function.
    	- [**`usePrevious`**](./src/usePrevious/index.ts) —
    		Returns the value passed to the hook on previous render.
    	- [**`usePreviousDistinct`**](./src/usePreviousDistinct/index.ts) —
    		Returns the most recent distinct value passed to the hook on previous renders.
    	- [**`useQueue`**](./src/useQueue/index.ts) —
    		A state hook implementing FIFO queue.
    	- [**`useRafState`**](./src/useRafState/index.ts) —
    		Like `React.useState`, but state is only updated within animation frame.
    	- [**`useRenderCount`**](./src/useRenderCount/index.ts) —
    		Tracks component's render count including first render.
    	- [**`useSet`**](./src/useSet/index.ts) — Tracks the
    		state of a `Set`.
    	- [**`useToggle`**](./src/useToggle/index.ts) — Like
    		`useState`, but can only be `true` or `false`.
    	- [**`useThrottledState`**](./src/useThrottledState/index.ts)
    		— Like `useState` but its state setter is throttled.
    	- [**`useValidator`**](./src/useValidator/index.ts)
    		— Performs validation when any of the provided dependencies change.
  • Navigator

    	- [**`useNetworkState`**](./src/useNetworkState/index.ts)
    		— Tracks the state of the browser's network connection.
    	- [**`useVibrate`**](./src/useVibrate/index.ts)
    		— Provides vibration feedback using the Vibration API.
    	- [**`usePermission`**](./src/usePermission/index.ts)
    		— Tracks the state of a permission.
  • Miscellaneous

    	- [**`useSyncedRef`**](./src/useSyncedRef/index.ts)
    		— Like `useRef`, but it returns an immutable ref that contains the actual value.
    	- [**`useCustomCompareMemo`**](./src/useCustomCompareMemo/index.ts)
    		— Like `useMemo` but uses provided comparator function to validate dependency changes.
    	- [**`useDeepCompareMemo`**](./src/useDeepCompareMemo/index.ts)
    		— Like `useMemo` but uses `@react-hookz/deep-equal` comparator function to validate deep
    		dependency changes.
    	- [**`useHookableRef`**](./src/useHookableRef/index.ts)
    		— Like `useRef` but it is possible to define handlers for getting and setting the value.
  • Side-effect

    	- [**`useAsync`**](./src/useAsync/index.ts)
    		— Executes provided async function and tracks its results and errors.
    	- [**`useAsyncAbortable`**](./src/useAsyncAbortable/index.ts)
    		— Like `useAsync`, but also provides `AbortSignal` as first function argument to the async
    		function.
    	- [**`useCookieValue`**](./src/useCookieValue/index.ts)
    		— Manages a single cookie.
    	- [**`useLocalStorageValue`**](./src/useLocalStorageValue/index.ts)
    		— Manages a single LocalStorage key.
    	- [**`useSessionStorageValue`**](./src/useSessionStorageValue/index.ts)
    		— Manages a single SessionStorage key.
  • Sensor

    	- [**`useIntersectionObserver`**](./src/useIntersectionObserver/index.ts)
    		— Observe changes in the intersection of a target element with an ancestor element or with the
    		viewport.
    	- [**`useMeasure`**](./src/useMeasure/index.ts) —
    		Uses `ResizeObserver` to track an element's dimensions and to re-render the component when they
    		change.
    	- [**`useMediaQuery`**](./src/useMediaQuery/index.ts)
    		— Tracks the state of a CSS media query.
    	- [**`useResizeObserver`**](./src/useResizeObserver/index.ts)
    		— Invokes a callback whenever `ResizeObserver` detects a change to the target's size.
    	- [**`useScreenOrientation`**](./src/useScreenOrientation/index.ts)
    		— Checks if the screen is in `portrait` or `landscape` orientation and automatically re-renders
    		on orientation change.
    	- [**`useDocumentVisibility`**](./src/useDocumentVisibility/index.ts)
    		— Tracks document visibility state.
  • Dom

    	- [**`useClickOutside`**](./src/useClickOutside/index.ts)
    		— Triggers a callback when the user clicks outside a target element.
    	- [**`useEventListener`**](./src/useEventListener/index.ts)
    		— Subscribes an event listener to a target element.
    	- [**`useKeyboardEvent`**](./src/useKeyboardEvent/index.ts)
    		— Invokes a callback when a keyboard event occurs on the chosen target.
    	- [**`useWindowSize`**](./src/useWindowSize/index.ts)
    		— Tracks the inner dimensions of the browser window.

Contributors

@wooldox/reservoir-kit-ui@beskar-labs/command-bar@beskar-labs/glimpse@beskar-labs/use-gamepad-events@beskar-labs/use-themeaidexa-open-dsraslib@rasreee/react-utils@infinitebrahmanuniverse/nolb-_react-h@textea/yodasjob-web-app@everything-registry/sub-chunk-764synapse-react-client@prezly/analytics-nextjs@prezly/content-renderer-react-js@prezly/theme-kit-uikedao@web-media/act-player@web-media/ap-preview@web-media/client-sdk@truvity/js-core__react-utils@tuci/pro@veecode-platform/core-components@veecode-platform/plugin-scaffolder@vrabbi/plugin-scaffolder@vrabbi/plugin-scaffolder-react@yearn-finance/web-lib@zoolabs/uiappskd@gqty/react@cloudbase/weda-ui@h5web/app@h5web/lib@edgeandnode/gds@haydenbleasel/use-gamepad-events@helms/weda-uitrain-brain-ui@backstage/plugin-entity-validation@backstage/plugin-auth-react@backstage/plugin-catalog-react@backstage/core-components@backstage/plugin-techdocs-module-addons-contrib@backstage/frontend-defaults@backstage/plugin-scaffolder@backstage/plugin-gcp-projects@backstage/plugin-scaffolder-react@backstage-community/plugin-odo-module-devfile-field-extension@backstage-community/plugin-entity-validation@backstage-community/plugin-gcp-projects@builtbymom/web3@chaibuilder/sdkyearn-lib-silent@haydenbleasel/command-bar@haydenbleasel/glimpse@haydenbleasel/use-sticky@haydenbleasel/use-theme@likec4/diagrams@likec4/diagram@moser-inc/moser-labs-react@nexm-io/reservoir-kit-ui@nftearth/reservoir-kit-uirea11y-easy-formreact-d3-calendar-heatmapreact-d3-treemappwa-kit-bedrockreact-glimpsenfttestzreservoir-kit-uinfttestzreservoir-kit-ui-2nfttestzreservoir-kit-ui-3nftfast2reservoir-kit-uireact-sync-boardreact-visual-noveldiditsdktestdidit-sdk@sh-reservoir0x/reservoir-kit-ui@techindicium/core-components@spear-ai/ui@fe-eule/react-keyboard-avoiding-view@factorioui/components@apptrium/web-host@andythehood/plugin-apigee@andythehood/plugin-gcp-projects@immobiliarelabs/backstage-plugin-ldap-auth@invexa/lib@invexa/react-lib@frontside/backstage-plugin-scaffolder-workflow@arthurcerqueira/core-components@diamondlightsource/davidia@mirohq/websdk-react-hooks@nlxai/touchpoint-ui@luma-health/ui-utils@simwrapper/h5web-app@pzh-ui/components@luxmarket/ui@origin/reservoir-kit-ui@sk-web-gui/alert-banner@saas-ui-pro/react@solq-ui/react@snowball-tech/fractal@solace-labs/backstage-plugin-ep-devp-auth
25.1.1

7 months ago

25.1.0

8 months ago

25.0.1

10 months ago

25.0.0

10 months ago

24.0.4

2 years ago

24.0.3

2 years ago

24.0.2

2 years ago

24.0.1

2 years ago

24.0.0

2 years ago

23.1.0

2 years ago

23.0.1

2 years ago

23.0.0

3 years ago

19.0.0

3 years ago

21.0.0

3 years ago

16.1.0

3 years ago

17.0.1

3 years ago

17.0.0

3 years ago

19.2.0

3 years ago

22.0.0

3 years ago

18.0.1

3 years ago

18.0.0

3 years ago

20.0.1

3 years ago

20.0.0

3 years ago

20.0.3

3 years ago

20.0.2

3 years ago

19.1.0

3 years ago

20.1.0

3 years ago

16.0.1

3 years ago

16.0.0

3 years ago

14.5.0

3 years ago

14.5.1

3 years ago

15.0.0

3 years ago

15.0.1

3 years ago

14.2.3

3 years ago

14.6.0

3 years ago

15.1.0

3 years ago

14.3.0

3 years ago

14.7.0

3 years ago

14.7.1

3 years ago

14.4.0

3 years ago

14.1.0

3 years ago

14.0.0

3 years ago

14.2.0

3 years ago

14.2.1

3 years ago

14.2.2

3 years ago

13.3.0

3 years ago

13.2.0

4 years ago

13.2.1

4 years ago

13.1.0

4 years ago

12.2.0

4 years ago

13.0.0

4 years ago

12.3.0

4 years ago

12.1.2

4 years ago

12.1.0

4 years ago

12.1.1

4 years ago

12.0.3

4 years ago

12.0.4

4 years ago

12.0.0

4 years ago

11.0.0

4 years ago

11.1.0

4 years ago

10.0.0

4 years ago

10.1.0

4 years ago

10.1.1

4 years ago

8.0.0

4 years ago

9.0.0

4 years ago

6.1.0

4 years ago

7.0.0

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.0

4 years ago

4.0.0

4 years ago

3.8.0

4 years ago

3.6.0

4 years ago

3.7.0

4 years ago

3.4.0

4 years ago

3.5.0

4 years ago

3.2.0

4 years ago

3.3.0

4 years ago

2.2.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.1.0

4 years ago

2.0.0

4 years ago

2.1.0

4 years ago

1.28.0

4 years ago

1.25.0

4 years ago

1.25.1

4 years ago

1.27.0

4 years ago

1.24.1

4 years ago

1.26.0

4 years ago

1.24.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.19.0

4 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.9.1

4 years ago

1.7.3

5 years ago

1.9.0

4 years ago

1.7.2

5 years ago

1.8.0

4 years ago

1.7.1

5 years ago

1.6.2

5 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.4.3

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.21.0

4 years ago

1.20.1

4 years ago

1.22.0

4 years ago

1.20.2

4 years ago

1.20.0

4 years ago

1.23.0

4 years ago

1.20.3

4 years ago

1.20.4

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.2.3

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago