JSPM

  • Created
  • Published
  • Downloads 248857
  • Score
    100M100P100Q178486F
  • License MIT

React hooks done right, for browser and SSR.

Package Exports

  • @react-hookz/web

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

NPM Version NPM Downloads NPM Dependents Build Coverage Types Tree Shaking

× DOCS × DISCORD × CHANGELOG ×


@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 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:

  1. Main, the /cjs folder — CommonJS modules, with ES5 lang level.
  2. ESM, the /esm folder — it is ES modules (browser compatible), with ES5 lang level.
  3. 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";

Hooks list

  • Callback

    • useDebounceCallback — Makes passed function debounced, otherwise acts like useCallback.
    • useRafCallback — Makes passed function to be called within next animation frame.
  • Lifecycle

  • State

    • useMediatedState — Like useState, but every value set is passed through a mediator function.
    • usePrevious — Returns the value passed to the hook on previous render.
    • useSafeState — Like useState, but its state setter is guarded against sets on unmounted component.
    • useToggle — Like useState, but can only become true or false.
    • useValidator — Performs validation when any of provided dependencies has changed.
  • Miscellaneous

    • useSyncedRef — Like useRef, but it returns immutable ref that contains actual value.
  • Side-effect

  • Sensor

    • useResizeObserver — Invokes a callback whenever ResizeObserver detects a change to target's size.
    • useMeasure — Uses ResizeObserver to track element dimensions and re-render component when they change.
  • Dom