英文:
Is there a way of having an undefined string passed to an Image in react native
问题
我目前有这段代码:
const renderImage = (imageUrl: string) => {
if (imageUrl)
return (<Image style={styles.image} source={require(imageUrl)} />);
return (<View></View>)
}
我收到的错误是: "在第28行发生无效调用: require(imageUrl)"。我原以为 "if(imageUrl)" 会解决这个问题。
我尝试过,但图片没有被渲染:
const renderImage = (imageUrl: string) => {
if (imageUrl)
return (<Image style={styles.image} source={{ uri: imageUrl }} />);
return (<View></View>)
}
编辑: 在加载数据时,imageUrl 将为 null,这是导致错误的原因。
英文:
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.
答案1
得分: 2
确保您从react native
中导入Image
并为宽度和高度添加样式:
<Image style={{ width: 100, height: 100 }} source={{ uri: "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1" }} />;
示例
import React from "react";
import { Image, ImageRequireSource, ImageURISource, View } from "react-native";
const imageURI = "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1";
const assetImage = require("./assets/images/icon.png");
const RenderURIImage = ({ imageURI }: { imageURI: ImageURISource["uri"] }) => {
return (
<Image source={{ uri: imageURI }} style={{ width: 100, height: 100 }} />
);
};
const RenderAssetImage = ({ image }: { image: ImageRequireSource }) => {
return <Image source={image} style={{ width: 100, height: 100 }} />;
};
function App() {
return (
<View style={{ flex: 1 }}>
<RenderURIImage imageURI={imageURI} />
<RenderAssetImage image={assetImage} />
</View>
);
}
请注意,这是您提供的代码的翻译部分。
英文:
make sure you import Image
from react native
and add styles for width and height:
<Image style={{ width: 100, height: 100 }} source={{uri : "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1"}} />;
Example
import React from "react";
import { Image, ImageRequireSource, ImageURISource, View } from "react-native";
const imageURI = "https://i.stack.imgur.com/RWR5C.jpg?s=256&g=1";
const assetImage = require("./assets/images/icon.png");
const RenderURIImage = ({ imageURI }: { imageURI: ImageURISource["uri"] }) => {
return (
<Image source={{ uri: imageURI }} style={{ width: 100, height: 100 }} />
);
};
const RenderAssetImage = ({ image }: { image: ImageRequireSource }) => {
return <Image source={image} style={{ width: 100, height: 100 }} />;
};
function App() {
return (
<View style={{ flex: 1 }}>
<RenderURIImage imageURI={imageURI} />
<RenderAssetImage image={assetImage} />
</View>
);
}
答案2
得分: 0
请确保您在图像上传递了尺寸,因为图像需要尺寸才能渲染,否则它永远不会渲染。您可以尝试像这样做:
<Image style={{ width: 200, height: 200 }} source={{ uri: imgUrl }} />;
还可以添加默认图像:
<Image style={{ width: 200, height: 200 }} source={{ uri: imgUrl || defaultImg }} />;
这样您可以确认发生了什么情况。希望它能正常工作。
英文:
Please make sure you are passing size to image because image need size to render otherwise it will never render. You can try something like this.
<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 what's going on. I hope it will work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论