Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Close a React Native Modal with a Button

import React, { useState } from 'react';
import { StyleSheet, Modal, Text, View, Pressable, TouchableOpacity } from 'react-native';

export default function App() {
  const [modalVisible, setModalVisible] = useState(true);
  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
      <Modal
        visible={modalVisible}
        onRequestClose={() => setModalVisible(false)}>
        <Pressable style={styles.outsideModal}
          onPress={(event) => { if (event.target == event.currentTarget) { 
            setModalVisible(false); } }} >
          <View style={styles.modal}>
            <View style={styles.modalHeader}>
              <View style={styles.modalHeaderContent}>
                 <Text>Other header content</Text></View>
              <TouchableOpacity onPress={() => setModalVisible(false)}>
                <Text style={styles.modalHeaderCloseText}>X</Text>
              </TouchableOpacity>
            </View>
            <View style={styles.modalContent}>
              <Text>
                Popup content.
              </Text>
            </View>
          </View>
        </Pressable>
      </Modal>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "white",
    alignItems: "center",
    justifyContent: "center",
  },
  modal: {
    flex: 1,
    margin: 50,
    padding: 5,
    backgroundColor: "white",
    shadowColor: "black",
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 4,
    elevation: 5,
  },
  /* The content of the modal takes all the vertical space not used by the header. */
  modalContent: {
    flex: 1,
    borderWidth: 1,
    borderColor: "black"
  },
  modalHeader: {
    flexDirection: "row",
    borderWidth: 1,
    borderColor: "black"
  },
  /* The header takes up all the vertical space not used by the close button. */
  modalHeaderContent: {
    flexGrow: 1,
  },
  modalHeaderCloseText: {
    textAlign: "center",
    paddingLeft: 5,
    paddingRight: 5
  },
  outsideModal: {
    backgroundColor: "rgba(1, 1, 1, 0.2)",
    flex: 1,
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: momentjs display timezone 
Javascript :: How to test useEffect in react testing library 
Javascript :: js time function 
Javascript :: javascript sort array descending order 
Javascript :: get n random items from array javascript 
Javascript :: how to flat an array in javascript iteratively 
Javascript :: generate random 6 digit number javascript 
Javascript :: mongoose save or update 
Javascript :: ajax mdn 
Javascript :: create dynamic element in javascript 
Javascript :: cors policy javascript 
Javascript :: javascript add event listenner for multiple events 
Javascript :: Removing Elements from End of a JavaScript Array 
Javascript :: import card content material ui 
Javascript :: op in sequelize 
Javascript :: scroll down or up event listener 
Javascript :: how to write a program that shows a random number between 1 and 100 in your browser 
Javascript :: javascript alert 
Javascript :: how to get last element of array in javascript 
Javascript :: javascript newline to brake 
Javascript :: send json body http get flutter 
Javascript :: react native password meter 
Javascript :: innertext of input js 
Javascript :: react native navigation nested 
Javascript :: adding function to objects js 
Javascript :: javascript online test 
Javascript :: int to string javascript 
Javascript :: html select specify deafult select by js variable 
Javascript :: javascript object loop 
Javascript :: js UTC to local timezone 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =