Close react native modal by clicking on overlay?

前端 未结 13 683
梦如初夏
梦如初夏 2020-12-23 14:27

Is it possible to close react native modal by clicking on overlay when transparent option is true? Documentation doesn\'t provide anything about it

13条回答
  •  情深已故
    2020-12-23 14:53

    Another solution:

    // Modal.js
    import React from 'react';
    import {
      TouchableWithoutFeedback,
      StyleSheet,
      Modal,
      View,
    } from 'react-native';
    import t from 'prop-types';
    
    
    class MyModal extends React.Component {
      static propTypes = {
        children: t.node.isRequired,
        visible: t.bool.isRequired,
        dismiss: t.func.isRequired,
        transparent: t.bool,
        animationType: t.string,
      };
    
      static defaultProps = {
        animationType: 'none',
        transparent: true,
      };
    
      render() {
        const { props } = this;
        return (
          
            
            
              
            
    
            
              {props.children}
            
            
          
        );
      }
    }
    
    
    const styles = StyleSheet.create({
      modalContent: {
        flex: 1,
        justifyContent: 'center',
        margin: '5%',
      },
      modalOverlay: {
        position: 'absolute',
        top: 0,
        bottom: 0,
        left: 0,
        right: 0,
        backgroundColor: 'rgba(0,0,0,0.5)'
      },
    });
    
    
    export default MyModal;
    

    Usage example:

    // SomeScreen.js
    import React from 'react';
    import { View, Text, Button } from 'react-native';
    
    import Modal from './Modal';
    
    
    class SomeScreen extends React.Component {
      state = {
        isModalVisible: false,
      };
    
      showModal = () => this.setState({ isModalVisible: true });
      hideModal = () => this.setState({ isModalVisible: false });
    
      render() {
        return (
          
            

提交回复
热议问题