【问题标题】:How can i show iconButton by using ternary operator如何使用三元运算符显示 iconButton
【发布时间】:2021-04-25 21:27:52
【问题描述】:

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

如何修复我的代码?

这是我的代码

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

    }


    console.log(imagePaths);

    // []  empty array

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

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2020-11-25
      • 2020-12-23
      • 2016-06-12
      • 2014-06-18
      • 2010-09-28
      相关资源
      最近更新 更多