Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

local image is being returned as an integer - React Native

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

like image 985
L-R Avatar asked Apr 25 '18 13:04

L-R


People also ask

How do I display local images in react native?

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.

How do I store an image in variable in react native?

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.

How do I bind an 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.


1 Answers

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")}/>
like image 140
L-R Avatar answered Sep 29 '22 19:09

L-R