Reputation: 1521
I already extracted data from firebase and displaying them. Now i need to display images also. In firebase database i have the same structure as the image below
And I have a folder under my project that contains all images with the exact same name like for an example "Carl.png" I extracted the photo content in and combined with the rest of the path : "Myproject/img/"+photo+".png" and it's displayed correctly in the screen. But when i try to affect it to image i get errors. And here is the code i tried
<Image style={{width: 50, height: 50}} source={{uri : (this.props.item.imageName)}}> </Image>
and i get this and if i try to put in
<Image style={{width: 50, height: 50}} source={{ uri: <Text> this.props.item.imageName </Text> }}> </Image>
i get this
and if i put this code
source={require(this.props.item.imageName)}
here is what i get: unknown named module
Upvotes: 2
Views: 1421
Reputation: 1521
actually, React native does not support the dynamic images even if they are stored locally and you just need to make their name dynamically variable. So, the only solution that i was obliged to use was to use a massive switch case and here the code i used:
switch (item.name) {
case 'carl':
return (
<Image source{require('pathUnderProject/carl.png')}/>
);
...
default:
return (
<View >
<Text>{'Null'}</Text>
</View>
);
I know it's not the best solution, but at least i'm sure it's working fine and it won't break in the production environment
Upvotes: 0
Reputation: 36
Try loading it using require because you are loading it from a local storage, so your source attribute becomes: source={ require('./img/'+photo+'.png') }
Upvotes: 1
Reputation: 127
try with
<Image style={{width: 50, height: 50}} source={{uri : (this.props.item.imageName)}} />
You've got space between opening & closing tag.
Upvotes: 0