[英]Trying to dismiss react-native modal when clicking outside of the box
所以我目前可以在框外单击时关闭模态,但问题是当我在框内单击时它仍然会关闭。 我尝试添加pointerEvents="none"
,但似乎不起作用。
这是我的代码:
<View>
<Modal
animationType="slide"
transparent={true}
style={{width: '100%', alignSelf: 'center', height: '100%', justifyContent: 'flex-start', backgroundColor:'green'}}
visible={this.state.modalVisible}
onRequestClose={() => {
alert('Modal has been closed.');
}}>
<TouchableWithoutFeedback onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<View style={{ backgroundColor: 'red', flex: 1}} >
<View pointerEvents="none" style={{alignSelf: 'center', width: '80%', height: '50%', backgroundColor: 'purple', top: 100}}>
<Text pointerEvents="none" >Hello World!</Text>
</View>
</View>
</TouchableWithoutFeedback>
</Modal>
</View>
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity,
Modal,
TouchableWithoutFeedback
} from 'react-native';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props)
this.state = { modalVisible: true }
}
setModalVisible(modalVisible) {
this.setState({ modalVisible })
}
render() {
return (
<View>
<Modal
animationType="slide"
transparent={true}
style={{ width: '100%', alignSelf: 'center', height: '100%', justifyContent: 'flex-start', backgroundColor: 'green' }}
visible={this.state.modalVisible}
onRequestClose={() => {
alert('Modal has been closed.');
}}>
<TouchableWithoutFeedback onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<View style={{ backgroundColor: 'red', flex: 1 }} >
<TouchableWithoutFeedback onPress={() => { }}>
<View style={{ alignSelf: 'center', width: '80%', height: '50%', backgroundColor: 'purple', top: 100 }}>
<Text>Hello World!</Text>
</View>
</TouchableWithoutFeedback>
</View>
</TouchableWithoutFeedback>
</Modal>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});a
因此,我开始使用它,它仅适用于react-native-modal。 react附带的模式不适用于此用例。
React Native提供的Modal
组件没有提供很多选项来控制其行为。 我建议您改用react-native-community / react-native-modal 。
通过使用react-native-community / react-native-modal和state管理可见性,这种方法为我解决了问题。 并且onBackdropPress
属性将起作用。 为使元素触发模态,应将状态设置为true。 见下面的代码
constructor(props){
super(props);
this.state = {
isVisible:false
}
<Modal
isVisible = {this.state.isVisible}
onBackdropPress = { () => this.setState({isVisible:false})}
backdropOpacity = {0.3}
style = {styles.modal}>
</Modal>
Setting the state directly from the Onpress event worked for me. that is instead of using this:
<TouchableWithoutFeedback onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
You can change to :
<TouchableWithoutFeedback onPress={() => {
this.setState({modalVisible:false})}}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.