Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@react-hook/resize-observer

jaredLunde1.2mMIT2.0.2TypeScript support: included

A React hook that fires a callback whenever ResizeObserver detects a change to its size

react, react hook, resize observer, resize observer hook, resize observer react hook, use-resize-observer, useresizeobserver, resize hook, size hook

readme


useResizeObserver()

Supported by FlexStack

Bundlephobia Types Build status NPM Version MIT License

npm i @react-hook/resize-observer

A React hook that fires a callback whenever ResizeObserver detects a change to its size.

Features

  • <input checked="" disabled="" type="checkbox"> Uses a single ResizeObserver for tracking all elements used by the hooks.
    [This approach is astoundingly more performant](https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/z6ienONUb5A/F5-VcUZtBAAJ)
    than using a `ResizeObserver` per element which most hook implementations do.
  • <input checked="" disabled="" type="checkbox"> Automatically unobserves the target element when the hook unmounts.
  • <input checked="" disabled="" type="checkbox"> You don't have to wrap your callback in useCallback() because any mutations
    are handled by the hook.

Quick Start

Check out an example on CodeSandbox

`jsx harmony import * as React from 'react' import useResizeObserver from '@react-hook/resize-observer'

const useSize = (target) => { const [size, setSize] = React.useState()

React.useLayoutEffect(() => { setSize(target.current.getBoundingClientRect()) }, [target])

// Where the magic happens useResizeObserver(target, (entry) => setSize(entry.contentRect)) return size }

const App = () => { const target = React.useRef(null) const size = useSize(target) return (

      {JSON.stringify({width: size.width, height: size.height}, null, 2)}
    
) }


## API

### useResizeObserver(target, callback, options)

```ts
function useResizeObserver<T extends Element>(
  target: React.RefObject<T> | React.ForwardedRef<T> | T | null,
  callback: UseResizeObserverCallback,
  options?: UseResizeObserverOptions
): ResizeObserver
Argument Type Required? Description
target React.RefObject<T> | T | null Yes A React ref created by useRef() or an HTML element
callback UseResizeObserverCallback Yes Invoked with a single ResizeObserverEntry any time the target resizes
options UseResizeObserverOptions No Options for the ResizeObserver instance.

Types

UseResizeObserverCallback

export type UseResizeObserverCallback = (
  entry: ResizeObserverEntry,
  observer: ResizeObserver
) => any

UseResizeObserverOptions

export type UseResizeObserverOptions = {
  polyfill?: any
}

polyfill

A ResizeObserver polyfill implementation such as @juggle/resize-observer (this was the default in V1.x).

import useResizeObserver from '@react-hook/resize-observer'
import {ResizeObserver} from '@juggle/resize-observer'

useResizeObserver(..., ..., { polyfill: ResizeObserver })

LICENSE

MIT