Created
September 24, 2021 18:23
-
-
Save alexrdz/fe50cb78e8daaf3c5016ece3ded913d0 to your computer and use it in GitHub Desktop.
get container dimensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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