Created
August 13, 2015 03:47
-
-
Save geta6/15bb423fb17cb7d2d8ec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 'use strict'; | |
| const _ = require('lodash'); | |
| const React = require('react'); | |
| module.exports = React.createClass({ | |
| displayName: 'ImageComponent', | |
| propTypes: { | |
| src: React.PropTypes.string.isRequired, | |
| alt: React.PropTypes.string.isRequired, | |
| retina: React.PropTypes.bool.isRequired | |
| }, | |
| getInitialState() { | |
| return {src: `/images/${this.props.src}`}; | |
| }, | |
| componentDidMount() { | |
| this.props.retina && window.devicePixelRatio > 1 && this.loadRetinaImage(); | |
| }, | |
| loadRetinaImage() { | |
| let src = this.state.src.replace(/(.+)(?:@2x)*\.(jpg|jpeg|png|gif})$/, '$1@2x.$2'); | |
| new Promise(resolve => _.extend(new Image(), {onload: resolve, src})).then(() => this.setState({src})); | |
| }, | |
| render() { | |
| return <img src={this.state.src} {..._.omit(this.props, ['src', 'retina'])} />; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment