react-native - 如何通过在 React Native 中点击模态主体来隐藏模态

标签 react-native modal-dialog overlay

我已经编写了一个代码来在用户单击模态中的“隐藏我”文本时隐藏模态,但我也希望当我们在模态外单击时隐藏模态。我可以使用“TouchableWithoutFeedback”,但它对我不起作用。我如何为此使用“TouchableWithoutFeedback”或任何其他解决方案?

this.state = {
 showPopupModal: false
}
  hideModal(){
   this.setState({showPopupModal: false});
  }  
showModal(){
  this.setState({showPopupModal: true});
 }  

return(
 <Conatiner>
  <View>
   <TouchableOpacity onPress={() => { showModal(); }}>
     <Text> open modal <Text>
   </TouchableOpacity>
</View>

<Modal visible={this.state.showPopupModal}>
  <View>
    <TouchableOpacity onPress={() => { hideModal(); }}>
      <Text> hide me </Text>
     </TouchableOpacity>
   </View>
 </Modal>
</Container>
);

最佳答案

您必须设置 Modal 的 isVisible 和 onBackdropPress 属性,例如:

  <Modal isVisible={this.state.showPopupModal} onBackdropPress={() => this.hideModal()} >
    ..
 </Modal>

关于react-native - 如何通过在 React Native 中点击模态主体来隐藏模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207237/

相关文章:

javascript - this.props undefined in fetch response-react native

twitter-bootstrap - 关闭 Bootstrap Modal 不会停止 JW Player

css - 为什么 css 不透明效果在 chrome 中不起作用?

javascript - 带画中画功能的 WebRTC 视频通话

javascript - 如何在这个函数调用中使 "modal conformation"功能起作用?

android - Android studio "AAPT2 error, failed parsing overlays"问题

android - 在 React Native 中获取设备 ID?

react-native - setOpacityTo() 与 <TouchableOpacity> 在 react-native

javascript - React native 清除 Stack Navigator 堆栈

python - 我如何从 django 中的 mysql 检索数据并以模式显示,其中我也可以更新它?