react native: How to set Image defaultSource on Android.
I have read the React native
1.58 document. I found the Image props defaultSource
supported iOS only.
I need to set the default Image when the network Image load error.
I used to write the code like this:
{ ImageUrl?
<Image style={styles.docimg}
source={{uri: ImageUrl}}/>
:
<Image style={styles.docimg}
source={require('../../../resource/default.png')}/>
}
Now there is a problem. When the URL is a string type, but it isn't a correct network Image. As the URL is true
then the Image will show nothing.
I saw the Image props onError
maybe solve my issue.
I need to set the placeholder Image.
The static images are added in app by placing it in somewhere in the source code directory and provide its path as: <Image source={require('./icon_name. png')} />
Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.
To resize image with React Native, we set the width and height of the image to percentages and set resizeMode to 'cover' . <View style={{ width: 180, height: 200, aspectRatio: 1 * 1.4, }} > <Image source={{ uri: item. image.
const [error, setError]=setState(false);
return(){
<Image
onError={(error) => {
setError(true);
}}
source={
error
? require("../../assets/images/defaultImage.png")
: { uri: imageUrl }
}
/>
}
This will show the default image if your network image fails to load or gives an error 404.
Work for me
<ImageBackground
style={{
width: 100,
height: 100,
marginRight: 20,
borderRadius: 10,
}}
source={
require('../../assets/images/Spinner.gif') //Indicator
}>
<Image
style={{
width: 100,
height: 100,
marginRight: 20,
borderRadius: 10,
}}
source={{
uri: `//Image U want To Show`
}}
/>
</ImageBackground>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With