useResizeObserver

    API

    import useResizeObserver from '@restart/hooks/useResizeObserver'
    • useResizeObserver<TElement>(element: TElement | null | undefined) => Rect | null

      Efficiently observe size changes on an element. Depends on the ResizeObserver api, and polyfills are needed in older browsers.

      const [ref, attachRef] = useCallbackRef(null);
      const rect = useResizeObserver(ref);
      return (
      <div ref={attachRef}>
      {JSON.stringify(rect)}
      </div>
      )

      Parameters

      • elementTElement | null | undefined

        The DOM element to observe

      Return Value Rect | null