Skip to content

Instantly share code, notes, and snippets.

@brayoh
Created April 11, 2019 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brayoh/720536760d0cc3edc2c8cc7fced37f26 to your computer and use it in GitHub Desktop.
Save brayoh/720536760d0cc3edc2c8cc7fced37f26 to your computer and use it in GitHub Desktop.
Get the width and height of an dom element in react
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