1.1.0 • Published 3 years ago

react-resize-hook v1.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
3 years ago

react-resize-hook

Resize observer hook for react with typescript support and resize observer polyfill.

Install

npm install --save react-resize-hook

or

yarn add react-resize-hook

How to

To watch for resizes on an a ref which contains an HTML element use:

...
import { useResizeObserver } from 'react-resize-hook'
...

const SomeFancyComponent = () => {
  const containerRef = React.useRef(null)
  const { width, height } = useResizeObserver({
    elementRef: containerRef,
  })

  return (
    <div ref={containerRef}>
      ...
    </div>
  )
}

...

To watch for resizes on a parent of the given ref one can use the parent level:

...
import { useResizeObserver } from 'react-resize-hook'
...

const SomeFancyComponent = () => {
  const containerRef = React.useRef(null)
  const { width, height } = useResizeObserver({
    elementRef: containerRef,
    parentLevel: 1, // this will use containerRef.current.parentElement
  })

  return (
    <div ref={containerRef}>
      ...
    </div>
  )
}

...

By default useResizeObserver watches for both width and height changes. You can only listen to width or height by adjusting the useResizeObserver params:

  useResizeObserver({
    elementRef: someRefWhichContainsADomElement,
    handleWidth: true, // or false
    handleHeight: true // or false
  })

You can also observe changes on the entire page by calling:

  useResizeObserver({
    watchEntirePage: true,
    handleWidth: true // or false,
    handleHeight: true // or false,
  })

Before the component is mounted (or before the first useEffect) width and height are undefined.

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago