Awesome Open Source
Awesome Open Source

react-resize-observer-hook Build Status

ResizeObserver + React hooks

Logo

Demo

zzarcon.github.io/react-resize-observer-hook

Install

yarn add react-resize-observer-hook

Usage

import {useState, useRef} from 'react';
import useResize from 'react-resize-observer-hook';

export default () => {
  const [dimensions, setDimensions] = useState({width: 100, height: 50})
  const parentRef = useRef(null);
  
  useResizeObserver(parentRef, (entry) => setDimensions({
    width: entry.width,
    height: entry.height,
  }));

  return (
    <div ref={parentRef}>
      {dimensions.width} x {dimensions.height}
    </div>
  )
}

API

DOMRectReadOnly {
  bottom: number;
  height: number;
  left: number;
  right: number;
  top: number;
  width: number;
  x: number;
  y: number;
  toJSON(): any;
}
ObserverCallback = (entry: DOMRectReadOnly) => void;
useResizeObserver = (ref: RefObject<HTMLElement>, callback: ObserverCallback) => {

Author

@zzarcon


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
typescript (12,062
react (5,571
hooks (271
react-hooks (216
resize (51
observer (25