javascript - 使用 navigation.getParam() React Native Navigation 设置图像名称
问题描述
我在 Stackoverflow 上的第一个问题 :)
我正在使用反应导航。如何通过 navigation.getParam('Country') 的值加载图像?此图像未加载(错误代码 500)
constructor(props) {
super(props);
this.state = { isLoading: true}
const { navigation } = this.props;
this.number = navigation.getParam('number');
this.country = navigation.getParam('country');
}
return (
<View style={bubble.container}>
<Text>{this.country}</Text>
<Image source={require('./img/flags/'+ this.country +'.png')} style={{width: 30, height: 30}}/>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <View style={bubble.wrapper}>
<Text selectable>{item.text}</Text>
<View style={bubble.footer}>
<View style={{flex: 1}}>
<Text style={bubble.sender}>{item.sender}</Text>
</View>
<View style={{flex: 1}}>
<Text style={bubble.time}><TimeAgo time={item.datetime} interval={20000}/></Text>
</View>
</View>
</View>}
/>
</View>
);
解决方案
首先,确保你的img在你的JS代码路径中可用,
假设您的 js 代码文件是Country.js
,因此,您的文件夹结构应该是:
--Country.js (File)
--img (folder)
--flags (folder)
--country.png (an image)
然后,尝试改变你的代码看起来像这样:
render(){
const {country} = this.props.navigation.state.params
return (
<View style={bubble.container}>
<Text>{country}</Text>
<Image source={require('./img/flags/'+ country +'.png')} style={{width: 30, height: 30}}/>
<FlatList
..your setting
/>
</View>
)
}
不需要声明params
in constructor
,可以声明param
of React Navigation
incomponentDidMount()/componentWillMount()/ even in render(){}
推荐阅读
- reactjs - 当我在 react.js 的页面之间移动时,如何让音乐播放器继续播放?
- xml - 最喜欢的电影以及不是 XML 和 Python 的电影的数量
- docker - 即使在我的 github 配置文件中创建并保存了公钥,远程 github 访问也被拒绝
- laravel - 如何将图像大小保存在数据库中?
- javascript - 无法读取未定义的属性“val”
- javascript - 如何将整个 obj 附加到另一个 obj 而不包装到数组中
- java - 通过Java程序连接Oracle
- docker - 如何将 ConfigMap 分配给已经在运行的 pod?
- java - Android 客户端和 Java 服务器“Java.net.ConnectException”
- c - 如何阻止 GCC 将此逐字节复制优化为 memcpy 调用?