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

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

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 ...