feat: basic auto sizer

This commit is contained in:
EnixCoda 2018-11-03 16:14:15 +08:00
parent 26b841921f
commit 82e7f94156
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD

View file

@ -0,0 +1,38 @@
import React from 'react'
import PropTypes from 'prop-types'
export default class AutoSizer extends React.Component {
static propTypes = {}
static defaultProps = {}
state = {
size: {
width: 0,
height: 0,
},
}
ref = React.createRef()
componentDidMount() {
const container = this.ref.current
if (container) {
this.setState({
size: {
width: container.offsetWidth,
height: container.offsetHeight,
},
})
}
}
render() {
const { size } = this.state
return (
<div {...this.props} ref={this.ref}>
{this.props.children(size)}
</div>
)
}
}