Skip to content

Instantly share code, notes, and snippets.

@mike-douglas
Created April 21, 2016 15:03
Show Gist options
  • Save mike-douglas/5ad8e1a7f1b6a78010fba1d1e390eb40 to your computer and use it in GitHub Desktop.
Save mike-douglas/5ad8e1a7f1b6a78010fba1d1e390eb40 to your computer and use it in GitHub Desktop.
import React from 'react'
import ReactDOM from 'react-dom'
export default class SquareImage extends React.Component {
constructor(props) {
super(props)
this.state = {
height: 0,
width: 0,
backgroundImage: `url(${props.backgroundImage})`
}
}
render() {
return <div
ref={(r) => this.node = r}
style={ this.state }></div>
}
resize() {
return (event) => this.modifyState({
height: window.innerWidth / Math.floor(window.innerWidth / this.props.minWidth),
width: window.innerWidth / Math.floor(window.innerWidth / this.props.minWidth)
})
}
componentDidMount() {
this.resize()()
window.addEventListener('resize', this.resize())
}
componentWillUnmount() {
window.removeEventLisener('resize', this.resize())
}
modifyState(newState) {
this.setState(Object.assign({}, this.state, newState))
}
}
SquareImage.defaultProps = {
minWidth: 200
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment