useSize()
npm i @react-hook/size
A React hook for measuring the size of HTML elements including when they change
Features
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 {
initialWidth: 0
initialHeight: 0
}
LICENSE
MIT