私は奇妙な問題に直面しています。私のリアクトネイティブアプリでは、onPress
イベントをView
に設定してもトリガーされませんが、View
内のText
に同じイベントを設定すると、トリガーされます。何が足りないのでしょうか?
<View style={{backgroundColor: "red", padding: 20}}>
<Text onPress={()=> {
console.log('works');
}
}>X</Text>
</View>
<View style={{backgroundColor: "red", padding: 20}} onPress={()=> {
console.log('does not work');
}
}>
<Text>X</Text>
</View>
これはなぜですか?これはReact Nativeの問題ですか?私はバージョン0.43を使っています。
TouchableWithoutFeedbackでビューをラップし、通常のように
onPressとフレンドを使用することができます。また、
pointerEvents`をブロックするには、子ビューの属性をonにします:
https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html
<TouchableWithoutFeedback onPressIn={this.closeDrawer}>
<Animated.View style={[styles.drawerBackground, styleBackground]} pointerEvents={isOpen ? undefined : 'none'} />
</TouchableWithoutFeedback>
TouchableOpacity、TouchableHighlight、TouchableNativeFeedbackを使用することができます。ビューコンポーネントはonPressをpropsとして提供しません。そのため、これらを代わりに使用します。