Created
April 21, 2016 15:03
-
-
Save mike-douglas/5ad8e1a7f1b6a78010fba1d1e390eb40 to your computer and use it in GitHub Desktop.
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
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