javascript - 如何使用三元运算符显示 iconButton
问题描述
当 imagePaths 通过三元运算符为空数组时,我想使 IconButton 不可见。但是我只想在数组包含这样的值时才显示iconButton:但是当我使用我的代码时,无论如何我都会看到iconButton。
我该如何修复我的代码?
这是我的代码
{imagePaths &&
<IconButton iconName="cancel" onPress={onRemoveImage} />
}
console.log(imagePaths);
// [] empty array
// ["SampleFile_1619356823623.jpg"] there is value in array
解决方案
您只是.length
用来检查数组是否有数据:
{imagePaths.length > 0 &&
<IconButton iconName="cancel" onPress={onRemoveImage} />
}
推荐阅读
- javascript - JavaScript:利用 setInterval() 调用类方法
- php - 其他元素未返回正确结果
- python - 向多个 csv 文件添加标题
- powershell - 在 PS 中动态设置属性
- java - 在 MySQL 数据库中搜索说该值已经存在而它不存在
- excel - 运行时错误“1004”-对象“_Global”的方法“范围”失败
- magento - Magento 2 varnish 不缓存,每次都提供新的 cookie
- asp.net-core - 使用用户提供的 oauth2 凭据配置 asp.net 核心身份 oauth2 身份验证
- jekyll - jekyll 项目中的自定义液体函数
- ios - 在“UIView”类型的对象上找不到属性“passwordRules”
*'