Skip to content

Instantly share code, notes, and snippets.

@jonathanharrell
Last active July 7, 2018 20:32
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 jonathanharrell/f4d6bd9651bceffa5fce588c6cad5ac7 to your computer and use it in GitHub Desktop.
Save jonathanharrell/f4d6bd9651bceffa5fce588c6cad5ac7 to your computer and use it in GitHub Desktop.
ObserveDimensions ReactComponent Using Render Props
class ObserveDimensions extends React.Component {
constructor(props) {
super(props)
this.state = {
width: null,
height: null
}
this.elementToObserve = React.createRef()
}
componentDidMount(nextProps) {
const erd = elementResizeDetectorMaker({ strategy: 'scroll' })
erd.listenTo(this.elementToObserve.current, element => {
this.setState({
width: element.offsetWidth,
height: element.offsetHeight
})
});
}
render() {
return (
<div className="observed-element" ref={this.elementToObserve}>
{this.props.render({
width: this.state.width,
height: this.state.height
})}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment