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

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...

Master Map & Filter, Javascript’s Most Powerful Array Functions

Master Map & Filter, Javascript’s Most Powerful Array Functions Learn how Array.map and Array.filter work by writing them yourself This article is for those who have written a  for  loop before, but don’t quite understand how  Array.map  or  Array.filter  work. You should also be able to write a basic function. By the end of this, you’ll have a complete understanding of both functions, because you’ll have seen how they’re written. Array.map Array.map  is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a new, transformed array. For example, say we have an array of numbers and we want to  multiply each number by three . We also don’t want to change the original array. To do this without  Array.map , we can use a standard for-loop. for-loop var originalArr = [1, 2, 3, 4, 5]; var newArr = []; for(var i = 0; i < originalArr.length; i+...