Last active
April 5, 2016 13:05
-
-
Save abhisharma2/b98d440e04c2a2a7f4d3fc9574aad80c to your computer and use it in GitHub Desktop.
Change img based on screen size
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
var React = require('react'); | |
var ReactDOM = require('react-dom'); | |
var DynamicImage = React.createClass({ | |
var width = $(window).width(); | |
getInitialState: function() { | |
if (width >= 1028) { | |
return {url: 'images/browser.png'}; | |
} else if (width < 1028 && width >=768) { | |
return {url: 'images/ipad.png'}; | |
} else { | |
return {url: 'images/iphone.png'}; | |
} | |
}, | |
handleResize: function(){ | |
if (width >= 1028) { | |
this.setState({url: "images/browser.png"}); | |
} else if (width < 1028 && width >=768) { | |
this.setState({url: "images/ipad.png"}); | |
} else { | |
this.setState({url: "images/iphone.png"}); | |
} | |
}, | |
componentDidMount: function() { | |
window.addEventListener('resize', this.handleResize); | |
}, | |
componentWillUnmount: function() { | |
window.removeEventListener('resize', this.handleResize); | |
}, | |
render: function() { | |
return ( | |
<img className="img-responsive" src={this.state.url}/> | |
); | |
} | |
}); | |
ReactDOM.render( | |
<DynamicImage/>, | |
document.getElementById('picture') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment