Last active
March 30, 2020 03:30
-
-
Save truongluu/89c4b1c563712fc7b42d1bd23186bfd4 to your computer and use it in GitHub Desktop.
PlaceHolderFastImage.js
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, { PureComponent } from 'react'; | |
import FastImage from 'react-native-fast-image'; | |
import { PLACEHOLDER_IMAGE } from '../const/app'; | |
export default class PlaceHolderFastImage extends PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = { | |
loading: true, | |
hasError: false | |
}; | |
} | |
onLoadEnd = () => { | |
this.setState({ loading: false }); | |
} | |
onLoadError = () => { | |
this.setState({ | |
hasError: true | |
}); | |
} | |
render() { | |
const { loading, hasError } = this.state; | |
const { source, style } = this.props; | |
return <> | |
{ | |
(loading || hasError) && <FastImage | |
source={{ uri: PLACEHOLDER_IMAGE }} | |
style={[style, { position: 'absolute', top: 0, left: 0 }]} | |
/> | |
} | |
<FastImage | |
source={source} | |
style={[style]} | |
onLoadEnd={this.onLoadEnd} | |
onError={this.onLoadError} | |
/> | |
</> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment