Upload local image after upload remote image

Is it possible to upload a local image if the remote image failed?

For example, I have the following code:

<Image style={ styles.userImage }
       source={ { uri: http://example.com/my_image.jpg } }
       onError={(error) => ...}
/>

In the case, for example, I do not have access rights to http://example.com/my_image.jpg, I will get an error in onError. Is there a way then to upload a local image?

+4
source share
1 answer

Use component state. In your constructor, set the start url:

 this.state = { image: { uri: 'http://example.com/my_image.jpg' } }

Create a handler onError:

 onError(error){
   this.setState({ image: require('your_local_image.path')})
 }

Then combine everything together:

 <Image style={ styles.userImage }
       source={ this.state.image }
       onError={ this.onError.bind(this) }
 />
+7
source

All Articles