Close react native modal by clicking on overlay?

前端 未结 13 658
梦如初夏
梦如初夏 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条回答
  •  -上瘾入骨i
    2020-12-23 14:38

    Simple solution with simple code, if you are using expo.
    Here is a complete component which can you just copy and paste and get it working.

    //MyModal.js
    
    import React from 'react';
    import { BlurView } from 'expo-blur';
    import { View, StyleSheet, Modal, TouchableWithoutFeedback } from 'react-native';
    
    export const MyModal = ({ children, visible, onRequestClose, onPressOverlay, }) => {
      return (
         
           
             
           
           
             {children}
           
        
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        height: '100%',
        width: '100%',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    

    Now you can import it into your work-space and use it like this.
    I'm using functional component and useState hook to show or hide the modal.

    //yourScreen.js
    
    import React, { useState } from 'react';
    import { View, Button } from 'react-native';
    import { MyModal } form './path/to/MyModal.js';
    
    const YourScreen = () => {
      const [modalVisible, setModalVisible] = useState(false);
      return (
        
          {
              setModalVisible(false);
            }}
            onPressOverlay={()=>{
              setModalVisible(!modalVisible);
            }}
          >
            // your modal content goes here
          
          

提交回复
热议问题