Created
April 11, 2019 09:09
-
-
Save brayoh/720536760d0cc3edc2c8cc7fced37f26 to your computer and use it in GitHub Desktop.
Get the width and height of an dom element in react
This file contains 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
class GenericHeightAndWidth extends Component { | |
state = { | |
elementWidth: 0, | |
elementHeight: 0 | |
}; | |
componentDidMount() { | |
const elementWidth = document.getElementById('element_id').clientWidth; // get element width | |
const elementHeight = document.getElementById('element_id').clientWidth; // get element height | |
this.setState({ elementWidth, elementHeight }); | |
} | |
render() { | |
const { elementWidth, elementHeight } = this.state; | |
return ( | |
<div height={elementHeight} width={elementWidth}> | |
// do whatever you want here | |
</div> | |
); | |
} | |
} | |
export default GenericModal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment