Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created June 23, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexFrazer/39f551f80c48caad5b56d6c140747024 to your computer and use it in GitHub Desktop.
Save AlexFrazer/39f551f80c48caad5b56d6c140747024 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import ImageError from 'app/components/image/ImageError';
type Props = {
src: string,
};
type State = {
hasError: boolean,
};
export default class Image extends Component {
props: Props;
state: State = {
hasError: false,
};
onErrror = () => this.setState({ hasError: true });
render() {
const { src, ...props } = this.props;
const { hasError } = this.state;
if (hasError) {
return (<ImageError />);
}
return (
<img
{...props}
src={src}
onError={this.onError}
/>
)
}
}
import React from 'react';
// placeholder component
export default function ImageError({}: Props) {
return (
<div>Something went wrong</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment