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

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

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

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...