React Native : View onPress가 작동하지 않습니다.
나는 이상한 문제에 직면 해있다. 내 네이티브 응용 프로그램을 반응에서 내가 설정 한 경우, onPress
이벤트를 View
이 트리거되지 않습니다하지만 난에 동일하게 설정하면 Text
내부 View
, 그것은 발생합니다. 내가 여기서 무엇을 놓치고 있습니까?
<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을 사용하고 있습니다.
당신은 사용할 수 있습니다 TouchableOpacity
위한 onPress
이벤트입니다. 소품을 View
제공하지 않습니다 onPress
.
<TouchableOpacity style={{backgroundColor: "red", padding: 20}} onPress={()=> {
console.log('does not work');
}
}>
<Text>X</Text>
</TouchableOpacity>
당신은으로보기를 래핑 할 수 TouchableWithoutFeedback
후 사용 onPress
과 친구들은 평소 좋아합니다. 또한 pointerEvents
자식 뷰에서 속성을 설정하여 차단할 수 있습니다 . 부모의 포인터 이벤트도 차단 TouchableWithoutFeedback
합니다. 흥미 롭습니다. 이것은 Android에서 필요했습니다. iOS에서는 테스트하지 않았습니다.
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를 소품으로 제공하지 않습니다. 그래서 당신은 이것 대신에 이것을 사용합니다.
<TouchableNativeFeedback
onPress={this._onPressButton}
</TouchableNativeFeedback>
OR
<TouchableHighlight onPress={this._onPressButton}>
</TouchableHighlight>
OR
<TouchableOpacity onPress={this._onPressButton}>
</TouchableOpacity>
참고 URL : https://stackoverflow.com/questions/43665177/react-native-view-onpress-does-not-work
'programing tip' 카테고리의 다른 글
Python 2.4에서 파일을 안전하게 열고 닫는 방법 (0) | 2020.10.05 |
---|---|
C 및 C ++에서 다르게 작동하는 열거 형 상수 (0) | 2020.10.05 |
JavaScript에서 다른 문자열의 모든 발생 색인을 찾는 방법은 무엇입니까? (0) | 2020.10.05 |
부모 상태 변경 후 React 자식 구성 요소가 업데이트되지 않음 (0) | 2020.10.05 |
C ++ 20에서 코 루틴은 무엇입니까? (0) | 2020.10.05 |