I'm trying to display a local image in my react native app. The docs say the way that you do this is by doing something like
<Image source={require('./my-icon.png')} />;
This works on its own, however I'm trying to pass this in as conditional logic if the api I'm getting data back from has a null url object.
<Image source={{uri: article.urlToImage || require("./awaiting.png")}}/>
When I do this I'm getting 'JSON value of '1' of type NSNumber cannot be converted to a valid URL'.
It seems to not like a combination of
<Image source={{uri: article.urlToImage }}/>
and
<Image source={require('./awaiting.png')} />
Can anyone help?
Thanks
To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop's value. to call require with a local image path. And then we set that as the source value to display the image.
Open android/app/src/main/assets folder from your App. js or index. js containing directory, if the assets folder doesn't exist create one. Make a folder named images or any NAME of your choice inside assets , and paste all the images there.
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.
Thanks for the help, the above solution didn't work but it let me on my way to this which does work
<Image source={urlToImage ? { uri: urlToImage } : require("./awaiting.png")}/>
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