Skip to content

Instantly share code, notes, and snippets.

@truongluu
Last active March 30, 2020 03:30
Show Gist options
  • Save truongluu/89c4b1c563712fc7b42d1bd23186bfd4 to your computer and use it in GitHub Desktop.
Save truongluu/89c4b1c563712fc7b42d1bd23186bfd4 to your computer and use it in GitHub Desktop.
PlaceHolderFastImage.js
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