Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native: How to set Image defaultSource on Android

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.

like image 587
jiexishede Avatar asked Jun 20 '17 05:06

jiexishede


People also ask

How do I set a static image in React Native?

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')} />

How do I display a png image in React Native?

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.

How do I resize an image in React Native?

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.


2 Answers


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.

like image 61
Harsh Mishra Avatar answered Sep 24 '22 22:09

Harsh Mishra


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>
like image 31
Irzam Tariq Avatar answered Sep 23 '22 22:09

Irzam Tariq