Created
June 18, 2020 07:35
-
-
Save wrumsby/5e98bc240668105c63aea5d2ab0054f2 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
import React from 'react'; | |
import Picture from './Picture'; | |
export const PictureWrapper = () => { | |
return ( | |
<div> | |
<Picture | |
src="http://placekitten.com/g/300/300" | |
srcSet="https://placeimg.com/440/440/nature 440w, https://placeimg.com/640/640/nature 640w" | |
sizes="(max-width: 500px) 440w, 640w" | |
/> | |
</div> | |
); | |
}; |
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
import * as React from 'react'; | |
import createClass from 'create-react-class'; | |
import PropTypes from 'prop-types'; | |
const Picture = createClass({ | |
displayName: 'Picture', | |
getInitialState() { | |
const { srcSet, sizes } = this.props; | |
return { | |
srcSet, | |
sizes | |
}; | |
}, | |
handleError() { | |
this.setState({ | |
srcSet: null, | |
sizes: null | |
}); | |
}, | |
render() { | |
const { srcSet, sizes } = this.state; | |
const { src, style } = this.props; | |
return ( | |
<div style={style}> | |
<img | |
src={src} | |
srcSet={srcSet} | |
sizes={sizes} | |
style={style} | |
onError={this.handleError} | |
/> | |
</div> | |
); | |
} | |
}); | |
Picture.propTypes = { | |
src: PropTypes.string.isRequired, | |
srcSet: PropTypes.string, | |
sizes: PropTypes.string, | |
style: PropTypes.object | |
}; | |
export default Picture; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment