在框外单击时尝试关闭react-native模态

wljmcqd8  于 2022-12-19  发布在  React
关注(0)|答案(5)|浏览(225)

所以我现在可以在点击框外时关闭模态,但问题是当我点击框内时它仍然关闭。我试过添加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>
2o7dmzc5

2o7dmzc51#

/**
 * 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
a7qyws3x

a7qyws3x2#

所以我让它工作了,它只适用于react-native-modal,react附带的modal不适用于这个用例。

pnwntuvh

pnwntuvh3#

React Native提供的Modal组件没有提供很多选项来控制其行为。我建议您尝试react-native-community/react-native-modal

b1zrtrql

b1zrtrql4#

这种方法解决了我使用react-native-community/react-native-modal和state来管理可见性的问题。并且属性onBackdropPress将起作用。对于触发模态的元素,您应该将state设置为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>
yks3o0rb

yks3o0rb5#

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})}}>

相关问题