forked from vivaxy/react-native-auto-height-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagePolyfill.js
33 lines (26 loc) · 996 Bytes
/
ImagePolyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, { useEffect } from 'react';
import { Platform, Image } from 'react-native';
import AnimatableImage from './AnimatableImage';
const isAndroid = () => Platform.OS === 'android';
/**
* An extension of the Image class which fixes an Android bug where remote images wouldn't fire the
* Image#onError() callback when the image failed to load due to a 404 response.
*
* This component should only be used for loading remote images, not local resources.
*/
function ImagePolyfill(props) {
const { source, onError, ...rest } = props;
const verifyImage = () => {
const { uri } = source;
Image.prefetch(uri).catch((e) => onError(e));
};
useEffect(() => {
if (source && source.uri && onError && isAndroid()) {
verifyImage();
}
}, [source, onError]);
return <AnimatableImage source={source} {...rest} />;
}
ImagePolyfill.propTypes = AnimatableImage.propTypes;
ImagePolyfill.defaultProps = AnimatableImage.defaultProps;
export default ImagePolyfill;