React-Native Image Invalid prop 'source' supplied to Image
This is not the recommended way to assign dynamically images since React Native must know all your images sources before compiling your bundle.
According to the docs, here's an example of how to load images dynamically:
// 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} />;
https://facebook.github.io/react-native/docs/images.html
Hope it helps
Edit: If you know all the images that could be loaded, you can try something like this:
// Create a file containing the references for your images
// images.js
const images = {
logo: {
uri: require('your-image-path/logo.png')
},
banner: {
uri: require('your-image-path/banner.png')
}
}
export { images };
//YourComponent.js
import { images } from 'yourImagesPath';
// for this test, expected to return [ { name: logo }, { name: banner} ]
const imagesFromTheServer = (your fetch);
imagesFromTheServer.map(image => {
if (!images[image]) {
return <Text>Image not found</Text>;
}
return <Image source={images[image].uri} />; // if image = logo, it will return images[logo] containing the require path as `uri` key
});
This is quite hacky but may work.
Let me know if it helped
As the React Native Documentation says, all your images sources needs to be loaded before compiling your bundle.
Adding a simple image in React Native should be like this:
<Image source={require('./path_to_your_image.png')} />
Let's say you have this:
const slides = {
car: './car.png',
phone: '../phone.png',
}
Then you pass slides
as a parameter but still, you won't be able to use it like this (even though logically should work):
<Image source={require(props.car)} />
Use require()
inside the sildes{}
const slides = {
car: require('./car.png'),
phone: require('../phone.png'),
}
And the use it like this:
<Image source={props.car} />