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

时间:2018-04-11 19:04:46

标签: javascript react-native

因此,当我在框外单击时,我能够解除模态,但问题是当我在框内单击时它仍然会解散。我尝试添加'b0,这似乎不起作用。

这是我的代码:

pointerEvents="none"

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

所以我让它工作,这只适用于react-native-modal。附带反应的模式不适用于此用例。

答案 2 :(得分:0)

React Native提供的

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

答案 3 :(得分:0)

通过使用react-native-community/react-native-modal和州来管理可见性,这种方式为我解决了。 属性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>