Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-hook/size

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-hook/size

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

  • 2.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
99K
increased by1.53%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • Determines the size of the element including any padding, borders, and scroll bars.
  • The size reported by the hook updates each time the size of the element changes.
  • 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.
  • Uses @juggle/resize-observer as a ponyfill when ResizeObserver isn't supported by the current browser.
  • 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?)

ArgumentTypeRequired?Description
targetReact.RefObject | T | nullYesA React ref created by useRef() or an HTML element
optionsUseSizeOptionsNoOptions 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

Keywords

FAQs

Package last updated on 21 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc