react-native - TypeError:未定义不是对象(评估'style.width')
问题描述
<TouchableOpacity style={styles.box}>
<ImageBackground
source={require('../images/pic.png')}
>
<Icon
type='foundation'
name='dump'
style={styles.icon}
/>
</ImageBackground>
<Text style={styles.text}>
Hello
</Text>
</TouchableOpacity>
嗨,我在使用 imagebackground 时不断收到 TypeError: undefined is not an object (evaluate 'style.width') 错误。为什么会出现这种情况?
解决方案
这是来自 imageBackground 的 RN 文档,它说您应该为图像背景添加高度和宽度以使其正常工作。
请注意,您必须指定一些宽度和高度样式属性。
因此,只需将您的代码替换为以下内容:
<TouchableOpacity style={styles.box}>
<ImageBackground
source={require('../images/pic.png')}
style={{width: '100%', height: '100%'}} // new addition
>
<Icon
type='foundation'
name='dump'
style={styles.icon}
/>
</ImageBackground>
<Text style={styles.text}>
Hello
</Text>
</TouchableOpacity>
推荐阅读
- python - 无法使用 Kafka(超过 1.3 MB)处理大消息?
- javascript - JQuery 在单击和悬停文本链接时显示 div - 更优雅的解决方案?
- google-apps-script - 有没有办法有条件地格式化从数组公式填充的单元格?
- angular - 将 Kendo Angular 网格列模板传递给托管网格的父组件
- sql - 如何编写 SQL 子查询?
- angular - Mock Angular Material MatCheckboxChanged Jasmine
- css - 相对于上方元素的位置和绝对到视口底部的位置
- php - Docker 映像在 AWS Fargate 上不起作用,但在 EC2 中起作用
- machine-learning - 我应该使用哪些功能来预测足球运动员的表现?
- java - Spring Boot JMS - 何时应在 @Transacted 方法上发送消息?