@react-hook/size
    TypeScript icon, indicating that this package has built-in type declarations

    2.1.2 • Public • Published

    useSize()

    Bundlephobia Types Build status NPM Version MIT License

    npm i @react-hook/size

    A React hook for measuring the size of HTML elements including when they change

    Features

    • [x] Determines the size of the element including any padding, borders, and scroll bars.
    • [x] The size reported by the hook updates each time the size of the element changes.
    • [x] Uses a single ResizeObserver for tracking all elements used by the hooks. This approach is astoundingly more performant than using a ResizeObserver per element which most hook implementations do.
    • [x] Uses @juggle/resize-observer as a ponyfill when ResizeObserver isn't supported by the current browser.
    • [x] Automatically unobserves the target element when the hook unmounts.

    Quick Start

    Check out an example on CodeSandbox

    import * as React from 'react'
    import useSize from '@react-hook/size'
    
    const Component = () => {
      const target = React.useRef(null)
      const [width, height] = useSize(target)
    
      return (
        <div ref={target}>
          <div>Width: {width}</div>
          <div>Height: {height}</div>
        </div>
      )
    }

    API

    useSize(target, options?)

    Argument Type Required? Description
    target React.RefObject | T | null Yes A React ref created by useRef() or an HTML element
    options UseSizeOptions No Options for SSR. See UseSizeOptions.

    UseSizeOptions

    export interface UseSizeOptions {
      // The initial width to set into state.
      // This is useful for SSR environments.
      initialWidth: 0
      // The initial height to set into state.
      // This is useful for SSR environments.
      initialHeight: 0
    }

    LICENSE

    MIT

    Install

    npm i @react-hook/size

    DownloadsWeekly Downloads

    36,879

    Version

    2.1.2

    License

    MIT

    Unpacked Size

    156 kB

    Total Files

    24

    Last publish

    Collaborators

    • jaredlunde