javascript - 如何使用状态制作动态图像 - 反应原生
问题描述
我的功能是:
getImage(page){
return
(
<Image
style={{position:'absolute',height:'60%',width:'90%',padding:50,bottom:30 ,alignSelf: 'center' }}
source={require('../../images/backgrounds/introduction/'+page.img)}
/>
)
}
render(){
return (
...
{this.getImage(activePage)}
)
}
但我收到此错误消息:
Error: /Users/***/components/login/Introduction.js:85:14: calls to `require` expect exactly 1 string literal argument, but this was found: `require('../../images/backgrounds/introduction/' + page.img)`.
解决方案
require()
你可以使用而不是串联state
。
初始化一个状态说imageArray={page1:require('link of page 1 image'),page2:require('link of page 2 image')}
然后像这样getImage()
使用,Image
<Image
style={{position:'absolute',height:'60%',width:'90%',padding:50,bottom:30 ,alignSelf: 'center' }}
source={this.state.imageArray[page]}
/>
其中page
是状态项之一的密钥imageArray
。
推荐阅读
- sql - 如何在sql中启动用户程序?
- python - 在 ARM64 上从 Windows 连接到 SQL Server 数据库
- javascript - 是否可以有两个 if 语句控制条件逻辑
- c - 为什么同一程序的 c 和 fortran 版本会产生不同的结果?
- android - @ionic-native/file/ngx TypeError:无法读取未定义的属性“then”
- asp.net-core - Serilog 弹性搜索
- c# - GetEnumerator 没有定义
- r - 使用两列按组排列行
- r - 在 Shiny 应用程序中构建 URL 搜索参数?
- html - Dart / Flutter - 有没有像 javascript 那样为 HTML 块获取 innerText?