Skip to content

Instantly share code, notes, and snippets.

@alexrdz
Created September 24, 2021 18:23
Show Gist options
  • Save alexrdz/fe50cb78e8daaf3c5016ece3ded913d0 to your computer and use it in GitHub Desktop.
Save alexrdz/fe50cb78e8daaf3c5016ece3ded913d0 to your computer and use it in GitHub Desktop.
get container dimensions
const useRefDimensions = (ref: React.RefObject<HTMLDivElement>) => {
const [dimensions, setDimensions] = useState({ width: 1, height: 2 })
useEffect(() => {
if (ref.current) {
const boundingRect = ref.current.getBoundingClientRect();
const { width, height } = boundingRect;
setDimensions({ width: Math.round(width), height: Math.round(height) })
}
}, [ref])
return dimensions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment