Skip to content

Instantly share code, notes, and snippets.

@builat
Created September 17, 2019 11:00
Show Gist options
  • Select an option

  • Save builat/4764df3ded03c91795003063b07b3dd1 to your computer and use it in GitHub Desktop.

Select an option

Save builat/4764df3ded03c91795003063b07b3dd1 to your computer and use it in GitHub Desktop.
Hook for getting rect bounds
type BoundsT = {
height: number
width: number
x: number
y: number
top: number
bottom: number
}
function useBounds<T>(): [LegacyRef<T>, BoundsT] {
const ref = useRef(null)
const [bounds, setBounds] = useState()
useLayoutEffect(() => {
setBounds(ref.current.getBoundingClientRect())
}, [ref])
return [ref, bounds]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment