Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react native modal close when click outside

<Modal transparent={true} visible={alert} animationType='slide' onRequestClose={() => setalert(false)}>
	<TouchableOpacity activeOpacity={1} style={Styles.center} onPressOut={() => setalert(false)}>
		<TouchableWithoutFeedback>
            {/* Somethings */}
		</TouchableWithoutFeedback>
	</TouchableOpacity>
</Modal >

{/*TouchableOpacity close the modal when click and the TouchableWithoutFeedback block the touch inside the rectangle*/}
{/*Code take from: https://stackoverflow.com/questions/40483034/close-react-native-modal-by-clicking-on-overlay*/}
Comment

Modal dismiss react native by click outside

// 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 (
            <View>
                <Modal
                    animationType="slide"
                    transparent={true}
                    visible={this.state.modalVisible}
                    onRequestClose={() => { this.setState({modalVisible: false})
                    }}
                  >
                    <TouchableOpacity style={styles.modalContainer} onPress={() => { this.setState({ modalVisible : false})}}>
                        <TouchableOpacity style={styles.modal} onPress={() => console.log('do nothing')} activeOpacity={1} >
                            Modal Content...
                        </TouchableOpacity>
                    </TouchableOpacity>
                </Modal>
            </View>
        )
    }
}


const styles = StyleSheet.create({
    modalContainer: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
    },
    modal: {
      width: 155,
      height: 300
    },
});

export default Modal;
Comment

PREVIOUS NEXT
Code Example
Javascript :: sort algorithm for array of objects in js 
Javascript :: save sort order of jquery sortable 
Javascript :: which line will generate a random number between 1 to 10 javascript 
Javascript :: why geting empty array from mongodb 
Javascript :: format iso time in human readable format with moment js 
Javascript :: check if value is array 
Javascript :: merge intervals 
Javascript :: Add array to formData react js 
Javascript :: node js express session expiration 
Javascript :: optional chaining 
Javascript :: discord bot remove message reaction 
Javascript :: post method in reactjs hooks. 
Javascript :: copy on clip board 
Javascript :: how to set asp radio button value to checked as per retrieve record in javascript 
Javascript :: convert json / array to excel in javascript 
Javascript :: sort array in javascript 
Javascript :: react routes multiple compoenents 
Javascript :: react native force vertical 
Javascript :: remove duplicate array 
Javascript :: alternative to setinterval 
Javascript :: try catch javascript 
Javascript :: private route in react js 
Javascript :: get query params 
Javascript :: right mouse click js 
Javascript :: array.contains javascript 
Javascript :: how to display words from an array in a box in javascript 
Javascript :: multiply js 
Javascript :: change text based on dropdown selection javascript 
Javascript :: remove equal json js 
Javascript :: trigger sweet alert through javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =