Can't load image from props - react-native

Somehow the location of my image is not found load it from props.
This works
let overview_bg = require('../../images/locaties/placeholder.png');
<Image source={overview_bg} style={styles.overview_bg}>
But when I get the url from props it doesn't:
let overview_bg = require(this.props.image);
<Image source={overview_bg} style={styles.overview_bg}>
this.props.image has the exact same url which gives me the error:
Requiring unknown module "../../images/locaties/placeholder.png". If
you are sure the module is there, try restarting the packager or
running "npm install"
I have checked the url multiple times it is exactly the same. I also tested it in console.log
The prop is given from another container which gets it from the store.

From the React Native Image documentation:
Note that in order for this to work, the image name in require has to be known statically.
// GOOD
<Image source={require('./my-icon.png')} />
// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('./' + icon + '.png')} />
// GOOD
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png');
<Image source={icon} />

Related

React Native Fast Image Caching

I am trying to cache profile pictures that are being fetched from an s3 bucket. On my backend I am able to fetch the url of each image. I then can loop through an array of profiles (which have the pictures' urls) and display the correct images, as used below.
{profiles && profiles.map((profile) => {
<FastImage
source={{
uri: profile.picture, // this is a URL string
cache: FastImage.cacheControl.immutable // Default cache
}}
/>
})}
When I change cache from 'immutable' to 'cacheOnly' the pictures are no longer rendered.
{profiles && profiles.map((profile) => {
<FastImage
source={{
uri: profile.picture, // this is a URL
cache: FastImage.cacheControl.cacheOnly // picture only renders if in cache
}}
/>
})}
But, if I substitute the picture variable with the actual URL string, the picture will render.
{profiles && profiles.map((profile) => {
<FastImage
source={{
uri: 'https://s3.aws.examplePictureKey'
cache: FastImage.cacheControl.immutable // Default cache
}}
/>
})}
Then if I change cache from 'immutable' to 'cacheOnly' the picture persists, telling me it has been cached. What I am wondering is:
Am I making a mistake here? All the documentation/resources I have read through do not have examples when looping through an array, but I am not sure why this would be the problem.
My understanding is that the image cached is based on the url string, if the url does not change FastImage just gets the image in the cache. But the url is not changing, so why wouldn't the image be saved in the cache?
The example above is slimmed down from what I am actually doing, the only difference though is that I am passing each profile element from a parent component before trying to render the image. Would this somehow prevent the image from being cached?
Documentation: https://www.npmjs.com/package/react-native-fast-image
Thanks!
The cache: FastImage.cacheControl.cacheOnly from FastImage means it wont do any network request, it will just try to find from the cache. Default(immutable) means it will only change when the url changes so on any subsequent re-render, if the url changes to anything by chance, it will do network request again. And you need to return the component from map as well and provide the width and height properties to FastImage style as well.
{Array.isArray(profiles) && profiles.map((profile) => {
return <FastImage
source={{
uri: profile.picture, // this is a URL string
// cache: FastImage.cacheControl.immutable // Default cache
}}
style={{width: 200, height: 200}}
/>
})}
Hope it helps.

Image is disappearing with same uri

I am running sample application. I am using amazon s3 to store the profile pics. So, I have to use the same url for the profile pictures. I give the Image a unique key prop to try and get it to reload the image, but it displays a previously uploaded image. It means cache is not cleared for image component.
I tried this below code. Image should be updated perfectly but any changes doing in that screen then image will be disappearing.
This issue only appears in android.
<Image
source={{uri: this.state.uri + '?' + new Date()}}
/>
Please refer this link:- enter link description here
Screenshot:-
set a local const variable as
const url = {uri: this.state.uri + '?' + new Date()};
Use the url as follows:
<Image
source={uri}
/>

Why my images aren't showing in React Native

I'm trying to show some images in my React Native app. It's a simple app, only for Android. The app get informations from an API. The initial View is a list of items and when I click in one item, it gets more info from the API and show in a new page. I also get some urls that points to images that are kept in a S3 bucket in AWS.
This is how I show the images:
<ScrollView>
{this.state.urls.map(url => {
<Image
source={{uri: url}}
style={{width:400, height:200}}
/>
}
</ScrollView>
The problem is that it's not showing those images. I can see that there is a part of the page that is for the image. I can see that because if a put the image first and after that some text, the text will appear at the bottom of the page. I don't know what is going on that the image doesn't appear.
I searched for an answer, found something about using https instead of http (at least for iOS). Found that I need to set width and height. I already did those things and others but it's not working.
I also tried just picking a image from the internet and putting it url direct in uri but it still doesnt't work.
You're missing a return statement in your map function.
<ScrollView>
{this.state.urls.map(url => {
return <Image
source={{uri: url}}
style={{width:400, height:200}}
/>
}
</ScrollView>
UPDATE
I just tried the url that you provided in the above comment and it doesn't work on my device. I tried a different image from the internet this one and it works. There may also be something that is stopping you from accessing the images from within a mobile app.
To debug further I try would a single <Image .../> with the image hosted on AWS and see if you can get that to show. There may need to be some AWS configuration that you need to change to allow the image to be accessed from a mobile device.
Assuming your image URLs are accessible in your application.
The below code will show your images. Actually you forgot to return a component from the map which is a must have.
<ScrollView>
{this.state.urls.map(url => {
return <Image
source={{uri: url}}
style={{width:400, height:200}} /> }
}
</ScrollView>

Possibility of loading Google places images in react-native Image component?

I am trying to find a way to load image reference I get from places api into places photo API using Image component of react-native like below,
const imageLink = 'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRaAAAAuZ36wzrMYCVNWM1smQkRKuQyXecPgRpcsMfrSqbJM0EUTXZ62j8Ljxtk8HsxohXbtAwZAwmj_F3HmDwLoQaH9qt2TZJhhA6EuXK41UFo0Ow750MJujtrr5hgHR2lF7EXEhCkWj_dZ4LefrYzLU1_5RF1GhTR7Ggem5IJ-v7V2U8fjKoU9tBUOg&key=--MyKey--'
return (
<View >
<TouchableHighlight >
<View>
<Image
style={{width:64, height:64,flex:1,backgroundColor:'red'}}
source={{uri: imageLink}}
/>
</View>
</TouchableHighlight>
</View>
);
I am getting only empty placeholder with above code. However, I am able to load this link in browser.One caveat is that google places photo's link is redirecting to some other url which then displays image in browser. Am I doing it right? or is there any other way I can accomplish the same. Note: I tried react-native-network-image library as well.
You can have a look at the ImagePrefetch function in googleplace module:
https://github.com/srirangan/googleplaces.js/blob/master/test/ImageFetchTest.js
However, I am having issue with the whole module at the moment because of the line var https = require("https"); called in this specific function.
Not sure if it's an issue with new version of RN (upgraded to 0.55 recently) or just some other error in my new setup.
This worked perfectly in previous RN (was running on 0.48)

React native inlineImageLeft value

I want to know the proper value for inlineImageLeft prop. in react native <TextInput> component. Can any one help me?
I copied the image in drawable folder.
<TextInput
autoCapitalize= {'words'}
autoFocus={true}
caretHidden = {false}
keyboardType={'url'}
defaultValue={'Hello!'}
placeholder={'hello world'}
placeholderTextColor= {'red'}
returnKeyType = {'send'}
secureTextEntry={true}
selectTextOnFocus={false}
inlineImageLeft="user"
/>
Create Drawable folder inside android res directory . Copy the image say image.png in that folder. Then use in TextInput component.
inlineImageLeft="image"
If image is not showing just uninstall the previous app and rerun the project.
From the React Native github page:
<TextInput
inlineImageLeft="ic_menu_black_24dp"
placeholder="This has drawableLeft set"
style={styles.singleLine}
/>
You can find an example in UIExplorer in react-native. In this example the inlineImageLeft prop used this way:
<TextInput inlineImageLeft="ic_menu_black_24dp"/>
Example is in:
react-native/Examples/UIExplorer/js/TextInputExample.android.js
It seems path should be relative to ./android/app/src/main/res/drawable/. Any other path does not work. Also image does not scale.
I also can't see the icon inside TextInput.
Then, I try to uninstall and reinstall App.
I got the build failed error:
What went wrong: Execution failed for task ':app:mergeDebugResources'.
C:\Users\yusuf\git\App3\android\app\src\main\res\drawable\search.ico:
Error: The file name must end with .xml or .png
Then, I changed my file type from .ico to .png
Image should be in the "drawable" folder.
<TextInput
style={styles.modalInput}
autoFocus ={true}
inlineImageLeft='search'
inlineImagePadding={5}
placeHolder ="Search Product"
placeholderTextColor="white"
onChangeText={ (text) => this.setState({text})}
value = {this.state.text}
/>
With the above change, it works now.