Skip to main content

React Native Modal

The React Native Modal is a type of View component which is used to present the content above an enclosing view. There are three different types of options (slide, fade and none) available in a modal that decides how the modal will show inside the react native app.
The Modal shows above the screen covers all the application area. To use the Modal component in our application, we need to import Modal from the react-native library.

Modal Props

PropsDescription
visibleThis prop determines whether your modal is visible.
supportedOritentionsIt allow for rotating the modal in any of the specified orientations (portrait, portrait-upside-down, landscape, landscape-left, landscape-right).
onRequestCloseThis is a callback prop which is called when the user taps on the hardware back button on Android or the menu button on Apple TV.
onShowThis allows passing a function which will show when the modal once visible.
transparentIt determines whether the modal will cover the entire view. Setting it to "true" renders the modal over the transparent background.
animationTypeIt controls how the modal animates. There are three types of animated props available:
slide: It slides the modal from the bottom.
fade: It fades into the view.
none: It appears the model without any animation.
hardwareAcceleratedIt controls whether to force hardware acceleration for the underlying window.
onDismissThis prop passes a function that will be called once the modal has been dismissed.
onOrientationChangeThis props is called when the orientation changes while the modal is being displayed. The type of orientation is "portrait" or "landscape".
presentationStyleIt controls the appearance of a model (fullScreen, pageSheet, fromSheet, overFullScreen) generally on the large devices.
animatedThis prop is deprecated. Use the animatedType prop instead, which is discussed above.

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.

App.js

  1. import React, {Component} from 'react';  
  2. import {Platform, StyleSheet, Text, View, Button, Modal} from 'react-native';  
  3.   
  4. export default class App extends Component<Props> {  
  5.   state = {  
  6.     isVisible: false//state of modal default false  
  7.   }  
  8.   render() {  
  9.     return (  
  10.       <View style = {styles.container}>  
  11.         <Modal            
  12.           animationType = {"fade"}  
  13.           transparent = {false}  
  14.           visible = {this.state.isVisible}  
  15.           onRequestClose = {() =>{ console.log("Modal has been closed.") } }>  
  16.           {/*All views of Modal*/}  
  17.               <View style = {styles.modal}>  
  18.               <Text style = {styles.text}>Modal is open!</Text>  
  19.               <Button title="Click To Close Modal" onPress = {() => {  
  20.                   this.setState({ isVisible:!this.state.isVisible})}}/>  
  21.           </View>  
  22.         </Modal>  
  23.         {/*Button will change state to true and view will re-render*/}  
  24.         <Button   
  25.            title="Click To Open Modal"   
  26.            onPress = {() => {this.setState({ isVisible: true})}}  
  27.         />  
  28.       </View>  
  29.     );  
  30.   }  
  31. }  
  32.   
  33. const styles = StyleSheet.create({  
  34.   container: {  
  35.     flex: 1,  
  36.     alignItems: 'center',  
  37.     justifyContent: 'center',  
  38.     backgroundColor: '#ecf0f1',  
  39.   },  
  40.   modal: {  
  41.   justifyContent: 'center',  
  42.   alignItems: 'center',   
  43.   backgroundColor : "#00BCD4",   
  44.   height: 300 ,  
  45.   width: '80%',  
  46.   borderRadius:10,  
  47.   borderWidth: 1,  
  48.   borderColor: '#fff',    
  49.   marginTop: 80,  
  50.   marginLeft: 40,  
  51.    
  52.    },  
  53.    text: {  
  54.       color: '#3f2949',  
  55.       marginTop: 10  
  56.    }  
  57. });  

Output:

React Native Modal React Native Modal

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...