Close react native modal by clicking on overlay?

前端 未结 13 653
梦如初夏
梦如初夏 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:36

    Simple solution. You need one touchableOpacity for clicking outside and another touchableOpacity for actual modal that will do nothing onPress.

    import React, { Component } from 'react'
    import { View, StyleSheet, TouchableOpacity, Modal} from 'react-native';
    
    export class Modal extends Component {
        constructor(props){
            this.state = { modalVisible : true}
        }
        render() {
            return (
                
                     { this.setState({modalVisible: false})
                        }}
                      >
                         { this.setState({ modalVisible : false})}}>
                             console.log('do nothing')} activeOpacity={1} >
                                Modal Content...
                            
                        
                    
                
            )
        }
    }
    
    
    const styles = StyleSheet.create({
        modalContainer: {
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
        },
        modal: {
          width: 155,
          height: 300
        },
    });
    
    export default Modal;
    

    activeOpacity={1} just removes touchableOpacity fade effect

提交回复
热议问题