javascript - 如何检查 React Native 中是否存在属性?

标签 javascript react-native

在 ReactNative 中,我有一个属性可能会也可能不会传递给类对象。有没有类似 Java 的方法来检查属性是否为 null?

在我的例子中,我有 this.props.overrideAccessibilityLabel 可能会被传递或不会被传递。我只想传下去的时候用:

  render() {
    return (
      <View 
        accessibilityLabel={this.props.currMessage.text}
        if {...props.myAccessibilityLabel} {
          ...accessibilityLabel={...props.myAccessibilityLabel}
        }
        >
        <Text1
           // ...
        >
        </Text1>
      </View>
    );
  }

最佳答案

但是在这种情况下,一行“if”语句最有效。

      <View 
        accessibilityLabel={this.props.currMessage.text ?
this.props.currMessage.text : this. props.myAccessibilityLabel}
        >
        <Text1
           // ...
        >
        </Text1>
      </View>

关于javascript - 如何检查 React Native 中是否存在属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45394495/

相关文章:

react :屏幕被切成两半

javascript - 如何在Tone.Player中使用音频缓冲区播放音调?

javascript - React-native-ui-lib 是否可用于 React Native 0.60? (安卓X)

react-native - 带有图像标签的可重用按钮(React Native)

react-native - 获取没有地理位置 API React-native 的国家/城市?

ios - react native iOS : Could not build module 'yoga' : 'algorithm' file not found

javascript - 将一个输入的值放入 AngularJS 中的另一个输入表单中

javascript - Node.js 服务器出现脚本和图像问题

javascript - 与 redux 一起使用像 SPA 这样的rest-api 的好解决方案吗?

javascript - 无法使用 jQuery 获取 HTML 表行值