forked from vivaxy/react-native-auto-height-image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: incorporated imagepolyfill in codebase, removed from deps
- Loading branch information
Showing
3 changed files
with
39 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useEffect } from 'react'; | ||
import { Image, Platform } from 'react-native'; | ||
|
||
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 <Image source={source} {...rest} />; | ||
} | ||
|
||
ImagePolyfill.propTypes = Image.propTypes; | ||
ImagePolyfill.defaultProps = Image.defaultProps; | ||
|
||
export default ImagePolyfill; |
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