首页 > 解决方案 > 如何使用三元运算符显示 iconButton

问题描述

当 imagePaths 通过三元运算符为空数组时,我想使 IconButton 不可见。但是我只想在数组包含这样的值时才显示iconButton:但是当我使用我的代码时,无论如何我都会看到iconButton。

我该如何修复我的代码?

这是我的代码

    {imagePaths &&
    <IconButton iconName="cancel" onPress={onRemoveImage} />

    }


    console.log(imagePaths);

    // []  empty array

    // ["SampleFile_1619356823623.jpg"]  there is value in array

标签: javascriptnode.jsreactjsreact-native

解决方案


您只是.length用来检查数组是否有数据:

{imagePaths.length > 0 &&
  <IconButton iconName="cancel" onPress={onRemoveImage} />
}

推荐阅读