Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss modal by tapping screen in ReactNative

How to dismiss the modal view by tapping screen in React Naitve, the RN Modal component seems do not provide the api

like image 578
Gerard Taub Avatar asked Jul 11 '16 16:07

Gerard Taub


People also ask

How do you dismiss a modal in react?

To close the modal, simply call the handleClose() function inside the onLoginFormSubmit() function body.

How do you prevent modal from closing when clicking outside react-native?

You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true.

How do I hide modal in react-native?

</Text> <Modal visible={modalVisible} onRequestClose={() => setModalVisible(false)}> <Pressable style={styles. outsideModal} onPress={(event) => { if (event. target == event. currentTarget) { setModalVisible(false); } }} > <View style={styles.

How do you open modal popup on button click in react-native?

React Native Modal Example Let's see an example of displaying a pop-up modal on clicking the button. Once we clicked the button, state variable isVisible sets to true and opens the Modal component. To implement the Modal component import Modal from the react-native library. onRequestClose = {() =>{ console.


1 Answers

You can use a TouchableWithoutFeedback component within the modal component with an onPress property that dismisses the modal.

<Modal visible={booleanThatHandlesModalVisibility}>
  <TouchableWithoutFeedback onPress={() => funcToHideModal()}>
    <View>
    ...
    </View>
  </TouchableWithoutFeedback>
</Modal>

If you want an area of the modal that doesn't hide the modal on press you can add another TouchableWithoutFeedback without an onPress property to catch the event before the first one like this:

<Modal visible={booleanThatHandlesModalVisibility}>
  <TouchableWithoutFeedback onPress={() => funcToHideModal()}>
    <View>
      <TouchableWithoutFeedback>
        <View>...</View>
      </TouchableWithoutFeedback>
    </View>
  </TouchableWithoutFeedback>
</Modal>
like image 73
Alvin Abia Avatar answered Sep 19 '22 14:09

Alvin Abia