react-native - 即使 JSON 文件没有链接,React Native 站点也会显示单词 Online
问题描述
我有这个移动应用程序页面,其中显示了我公司提供的服务及其地址。如果服务有网址,则应在移动应用程序上显示“在线”一词,如果没有,则不应显示在线一词。我正在使用 flatlist 显示服务。以下是我的代码的一部分,如果服务存在或不存在,则显示“在线”字样。
handleClick = (link) => {
Linking.canOpenURL(link).then(supported => {
if (supported) {
Linking.openURL(link);
} else {
console.log('Don\'t know how to open URI: ' + link);
}
});
};
<View style={styles.AddressRow}>
{
item.Online != ''? <TouchableOpacity onPress={() => Linking.openURL( item.Online )}>
</TouchableOpacity>: null
}
<TouchableOpacity onPress={() => Linking.openURL(item.Online)}>
<Text style={styles.underLineText}>Online</Text>
</TouchableOpacity>
我的 JSON 文件如下所示:
[
{
"id":"1",
"fk": 1,
"addr": "Test Drive",
"phone": "951-955-6200",
"LatL":"33.9",
"Long2":"-117.373423",
"Online": "http://www.test.com",
"image" : "png"
},
{
"id":"3",
"fk": 1,
"addr": "1234 Test drive",
"phone": "951-955-6200",
"LatL":"33.7",
"Long2":"-116.971169",
"Online": "",
"image" : "Home"
}
]
即使 JSON 文件中的 Online 为空,除了在线单词显示之外,所有内容都正确显示:“在线”:“”,
任何帮助将不胜感激。
解决方案
如果使用逻辑运算符,您似乎正在尝试进行内联
<View style={styles.AddressRow}>
{item.Online !== '' && <TouchableOpacity onPress={() => Linking.openURL(item.Online)}>
<Text style={styles.underLineText}>Online</Text>
</TouchableOpacity>}
https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator
或者,您可以使用条件运算符执行内联 If-Else
<View style={styles.AddressRow}>
{item.Online !== '' ? (<TouchableOpacity onPress={() => Linking.openURL(item.Online)}>
<Text style={styles.underLineText}>Online</Text>
</TouchableOpacity>) : null}
https://reactjs.org/docs/conditional-rendering.html#inline-if-else-with-conditional-operator
推荐阅读
- javascript - 检查用户和机器人角色是否高于提到的用户(discord.js)
- android - 使用 PassportJS 响应本机社交登录
- ios - Apple Silicon M1 Mac Mini 上的 Flutter:iOS SIM 错误
- scala - 杰克逊用 $ 符号读取节点字段
- npm - 为什么每次我使用 npm 或 yarn 安装包时,它都会安装所有依赖项?
- rust - 迭代器表示任意行为
- javascript - IOS SAP Hybrid/Native 移动应用程序中的 ajax GET 调用中的禁止错误
- python - NN 如何理解哪个类对应于哪个输出节点?反过来,我们作为人们如何识别概率对应的内容?
- python - 为什么某些动画 GIF 会在 Pillow 上显示故障区域?
- vue.js - 插入数据正在工作,但是当我更新数据时,它会给出一个错误,即所有字段都是必需的,即使它有一个值。如何解决这个问题?