I currently have this bit of code :
const renderImage = (imageUrl: string) => {
if (imageUrl)
return (<Image style={styles.image} source={require(imageUrl)} />);
return (<View></View>)
}
and the error I am getting is : "Invalid call at line 28: require(imageUrl)". I would have thought that the "if(imageUrl)" would have sorted that out.
I have tried but the image is not being rendered:
const renderImage = (imageUrl: string) => {
if (imageUrl)
return (<Image style={styles.image} source={{ uri: imageUrl }} />);
return (<View></View>)
}
EDIT: The imageUrl will be null while loading data which is what is causing the error.
2
Answers
make sure you import
Image
fromreact native
and add styles for width and height:Example
Please make sure you are passing sizes to image cause image need size to render otherwise it will never render.
Like this you can add.
<Image style={{ width: 200, height: 200 }} source={{uri : imgUrl}} />;
And also you can add default image.
<Image style={{ width: 200, height: 200 }} source={{uri : imgUrl || defaultImg }} />;
So you can confirm it whats going on. I hope it will works.