Created
June 23, 2017 17:19
-
-
Save AlexFrazer/39f551f80c48caad5b56d6c140747024 to your computer and use it in GitHub Desktop.
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
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} | |
/> | |
) | |
} | |
} |
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
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