【发布时间】:2018-06-03 15:24:44
【问题描述】:
我正在尝试通过发送标题和图像 URL 来呈现来自基本组件的图像列表。
如果我像这样渲染图像,一切正常:
<Image source={require('../assets/images/myImage.png')} />
当我尝试从道具渲染图像时,问题来了:
class ListItem extends Component {
render() {
return (
<View>
<Text>{this.props.title}<Text>
<Image source={require(this.props.imageUri)} />
</View>
);
}
};
ListItem.propTypes = {
title: PropTypes.string.isRequired,
imageUri: PropTypes.string.isRequired,
};
结果,我收到下一个错误:
对
require的调用期望恰好有 1 个字符串文字参数,但这 已找到:require(this.props.imageUri)。
我也尝试使用 uri 渲染图像
<Image source={{uri: this.props.imageUri}} />
package.js
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-27.0.0.tar.gz",
【问题讨论】:
-
你试过
source={this.props.imageUri}吗?this.props.imageUri的示例值是多少?您是在尝试使用相对路径还是绝对路径? -
检查stackoverflow.com/questions/36460827/…看看是否有帮助。
-
source={this.props.imageUri}- 是的,我试过了。我没有收到任何错误,但没有渲染图像。this.props.imageUri = ../assets/images/myImage.png -
And check stackoverflow.com/questions/36460827/… to see if it helps- 这无济于事,因为我不想将图像存储到state中。大约有 100 种可能的图像组合在未来可能会发生变化。我正在寻找从 Json/APi 发送路径 -
你不能写这样的动态 require 语句,它在错误中说,它想要一个字符串文字
标签: reactjs react-native