Skip to content

Instantly share code, notes, and snippets.

@wrumsby
Created June 18, 2020 07:35
Show Gist options
  • Save wrumsby/5e98bc240668105c63aea5d2ab0054f2 to your computer and use it in GitHub Desktop.
Save wrumsby/5e98bc240668105c63aea5d2ab0054f2 to your computer and use it in GitHub Desktop.
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>
);
};
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