John David

John David

1580920380

How to Work with React Native Modal Component

A comprehensive step by step tutorial on how to work with React Native Modal component. In this tutorial, we will learn how to show essential content such as text and image with Modal popup in React Native application.

React Native Modal Tutorial with Examples

A Modal is a pre-defined component that helps in creating the modal popup to React Native. Ordinarily, a Modal component is a primary way to present content above an enclosing view.

If you need more control over how to present modals over the rest of your app, then consider using a top-level Navigator.

Working with modal popups has been made easy by built-in React Native Modal component. In this tutorial, we will check out the various react-native modal examples, and show you how you can add different controls over the modal component to customize it to the next level.

React Native Modal Example

Prerequisite

Following tools, frameworks, and packages required to get started with this tutorial.

  • Node.js
  • NPM
  • React-Native
  • React-Native CLI
  • Text Editor or IDE
  • Xcode or Android Studio

Initiate React Native Project

Run command in the terminal to globally Install the latest React Native CLI version.

npm install -g react-native-cli

Let’s start installing the React Native app first.

react-native init modalreactnative

Head over to the project directory.

cd modalreactnative

Run the command to check the installed React Native version:

react-native -v

# react-native-cli: 2.0.1
# react-native: 0.61.5

Run React Native App with Xcode Emulator

The Xcode IDE is at the center of the Apple development experience. Robustly blended with the Cocoa and Cocoa Touch frameworks, Xcode is an astonishingly productive environment for developing apps for Mac, iPhone, iPad, Apple Watch, and Apple TV.

Here is the process to download Xcode from Apple site.

  • Visit Apple Developer
  • Click on develop
  • Click on downloads and sign in
  • Scroll to the bottom and click see more downloads
  • Use the search bar to search up Xcode
  • Click the little plus icon on the row with the Xcode version you want
  • Click download on the zip file

Or even you can visit the following URL to download Xcode.

Next, set the Xcode path for command line tool. Go to Xcode > Preferences > Locations Here set the Command Line Tools: Xcode xx.x.x (version number) from the dropdown.

Run React Native App in Xcode Emulator

Next, run the following command to install cocoapods.

sudo gem install cocoapods

Next, get inside the ios folder of your project.

cd ios

Now, run the command while staying in the ios folder.

pod install

Then, we are all set with configuration, come out from the iOS folder and run the command in the terminal to start the app in the iOS emulator using Xcode.

react-native run-ios

demo modal react native

To run your app in an Android device or emulator, you have to set the android development environment in your machine and run the following command.

react-native run-android

Add Button to Open Modal Dialog

We are about to add a cool stylish button in our react-native application, and this button will be responsible for opening the Modal when the user clicks on this button.

However, we are not going to use the Button component. Instead, we will use the TouchableOpacity component to create and add a custom styling in a button.

Open the App.js file and replace with the following code.

// App.js

import React, { Component } from 'react';
import { 
  View,
  Text,
  StyleSheet,
  TouchableOpacity
} from 'react-native';

export default class App extends Component {

  render() {
    return (
      <View style = { styles.container }>
         <TouchableOpacity
              style={styles.button}
              onPress={() => {
                this.displayModal(true);
              }}>
              <Text style={styles.buttonText}>Show Modal</Text>
          </TouchableOpacity>          
        </View>
      );
  }
};

const styles = StyleSheet.create({
  container: {
    padding: 25,
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    display: 'flex',
    height: 60,
    borderRadius: 6,
    justifyContent: 'center',
    alignItems: 'center',
    width: '100%',
    backgroundColor: '#2AC062',
    shadowColor: '#2AC062',
    shadowOpacity: 0.5,
    shadowOffset: { 
      height: 10, 
      width: 0 
    },
    shadowRadius: 25,
  },  
});

Show React Native Modal

In this step we will learn how to implement Simple Modal in React Native with some content (Image and Text). Open the App.js file and place the following code in it.

// App.js

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

export default class App extends Component {
  // initial state
  state = {
    isVisible: false
  };

  // hide show modal
  displayModal(show){
    this.setState({isVisible: show})
  }

  render() {
    return (
      <View style = { styles.container }>
        <Modal
            animationType = {"slide"}
            transparent={false}
            visible={this.state.isVisible}
            onRequestClose={() => {
              Alert.alert('Modal has now been closed.');
            }}>

            <Image 
              source={require('./assets/scooby.jpeg')}
              style = { styles.image }/>
              <Text style = { styles.text }>
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                  Maecenas eget tempus augue, a convallis velit.</Text>
          </Modal>

          <TouchableOpacity
              style={styles.button}
              onPress={() => {
                this.displayModal(true);
              }}>
              <Text style={styles.buttonText}>Show Modal</Text>
          </TouchableOpacity>          
        </View>
      );
  }
};

const styles = StyleSheet.create({
  container: {
    padding: 25,
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    display: 'flex',
    height: 60,
    borderRadius: 6,
    justifyContent: 'center',
    alignItems: 'center',
    width: '100%',
    backgroundColor: '#2AC062',
    shadowColor: '#2AC062',
    shadowOpacity: 0.5,
    shadowOffset: { 
      height: 10, 
      width: 0 
    },
    shadowRadius: 25,
  },
  closeButton: {
    display: 'flex',
    height: 60,
    borderRadius: 6,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FF3974',
    shadowColor: '#2AC062',
    shadowOpacity: 0.5,
    shadowOffset: { 
      height: 10, 
      width: 0 
    },
    shadowRadius: 25,
  },
  buttonText: {
    color: '#FFFFFF',
    fontSize: 22,
  },
  image: {
    marginTop: 150,
    marginBottom: 10,
    width: '100%',
    height: 350,
  },
  text: {
    fontSize: 24,
    marginBottom: 30,
    padding: 40,
  }
});

We imported the Modal component along with Image and Text components.

We defined the initial state of the Modal, the displayModal() is a function that set the Modal state to true if its false.

In the following react native modal example, we implemented the animated Modal by just setting the animation property to ‘slide’. Apart from that, you can also choose ‘fade’ or ‘none’ prop to animate the modal.

To set the React Native Modal’s transparent background, we set the transparent property to false.

We are showing The image and text in the Modal and added the style for the components using { styles.className } property.

Close React Native Modal Popup

Next, we will understand how to close the React Native Modal, add the following code inside the Modal component.

<Text style={styles.closeText}
  onPress={() => {
   this.displayModal(!this.state.isVisible);}
  }> Close Modal </Text>

Here is the style for close button.

const styles = StyleSheet.create({
  closeText: {
    fontSize: 24,
    color: '#00479e',
    textAlign: 'center',
  }
});

And, here is the final App.js code.

// App.js

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

export default class App extends Component {
  // initial state
  state = {
    isVisible: false
  };

  // hide show modal
  displayModal(show){
    this.setState({isVisible: show})
  }

  render() {
    return (
      <View style = { styles.container }>
        <Modal
            animationType = {"slide"}
            transparent={false}
            visible={this.state.isVisible}
            onRequestClose={() => {
              Alert.alert('Modal has now been closed.');
            }}>

            <Image 
              source={require('./assets/scooby.jpeg')}
              style = { styles.image }/>
              <Text style = { styles.text }>
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                  Maecenas eget tempus augue, a convallis velit.</Text>

              <Text 
                style={styles.closeText}
                onPress={() => {
                  this.displayModal(!this.state.isVisible);}}>Close Modal</Text>
          </Modal>

          <TouchableOpacity
              style={styles.button}
              onPress={() => {
                this.displayModal(true);
              }}>
              <Text style={styles.buttonText}>Show Modal</Text>
          </TouchableOpacity>          
        </View>
      );
  }
};

const styles = StyleSheet.create({
  container: {
    padding: 25,
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    display: 'flex',
    height: 60,
    borderRadius: 6,
    justifyContent: 'center',
    alignItems: 'center',
    width: '100%',
    backgroundColor: '#2AC062',
    shadowColor: '#2AC062',
    shadowOpacity: 0.5,
    shadowOffset: { 
      height: 10, 
      width: 0 
    },
    shadowRadius: 25,
  },
  closeButton: {
    display: 'flex',
    height: 60,
    borderRadius: 6,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FF3974',
    shadowColor: '#2AC062',
    shadowOpacity: 0.5,
    shadowOffset: { 
      height: 10, 
      width: 0 
    },
    shadowRadius: 25,
  },
  buttonText: {
    color: '#FFFFFF',
    fontSize: 22,
  },
  image: {
    marginTop: 150,
    marginBottom: 10,
    width: '100%',
    height: 350,
  },
  text: {
    fontSize: 24,
    marginBottom: 30,
    padding: 40,
  },
  closeText: {
    fontSize: 24,
    color: '#00479e',
    textAlign: 'center',
  }
});

Modal Methods & Properties

There are various other properties which can allow us to customize Modal to some extent. We will have a look at the useful methods and properties to work with Modal. However, you can visit the following URL to check out the full details about Modal props.

Visible – This property makes sure whether your modal should be visible or not. It takes two parameters, either true or false.

supportedOrientations: The supportedOrientations prop lets the modal to be wheeled to any of the specified orientations.

On iOS, the modal is still restricted by what’s specified in your app’s Info.plist’s UISupportedInterfaceOrientations field. When using presentationStyle of pageSheet or formSheet, this property will be ignored by iOS.

animationType: The animationType prop manages how modal should be animated. However, the default prop is set to be none.

  • fade into view
  • slides in from the bottom
  • shows without animation

hardwareAccelerated: This property examines whether to enforce hardware acceleration for the underlying window.

presentationStyle: This value examines how the modal should appear (usually on larger devices like iPad or larger iPhones).

  • fullScreen comprises the screen perfectly
  • formSheet overlays narrow-width composition centered
  • pageSheet covers portrait-width view centered (exclusively on larger devices)
  • overFullScreen includes the screen effectively but concedes clarity

React Native Basic Modal

Have you thought of a modal that we can observe in a lot of simplistic applications? In such cases, the perceptibility of the modal is handled privately by some component’s local state.

Possibly something similar to this:

class ModalDialog extends Component {
  state = { 
    isConfirmed: false 
  };

  submitEvent = value => {
    this.setState({ isConfirmed: true, value: value });
  };

  // Handler when user tries to confirm choices in modal popup
  confirmEvent = () => {
    // …
  };

  // Handler when user tries to cancel confirmation modal popup
  cancelEvent = () => {
    this.setState({ isConfirmed: false });
  };

  render() {
    const { isConfirmed } = this.state;

    return (
      <View style={styles.container}>
	     {isConfirmed && <Dialog onCancel={this.cancelEvent} onConfirm={this.confirmEvent} />}
      </View>
    );
  }
}

export default ModalDialog;

Conclusion

Thats it for now, finally we have completed React Native Modal tutorial in this tutorial we have learned how to display modal in iOS and Android app with some data. We have learned how to show and hide modal popup in React native app and also focused on basic styling of components using style.classname property in React Native.

You can get the full code of this tutorial on this GitHub repo.

Happy Coding!

#react-native #mobile-app

What is GEEK

Buddha Community

How to Work with React Native Modal Component
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Juned Ghanchi

1621573085

React Native App Developers India, React Native App Development Company

Expand your user base by using react-native apps developed by our expert team for various platforms like Android, Android TV, iOS, macOS, tvOS, the Web, Windows, and UWP.

We help businesses to scale up the process and achieve greater performance by providing the best react native app development services. Our skilled and experienced team’s apps have delivered all the expected results for our clients across the world.

To achieve growth for your business, hire react native app developers in India. You can count on us for all the technical services and support.

#react native app development company india #react native app developers india #hire react native developers india #react native app development company #react native app developers #hire react native developers

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms?

React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. Hire Dedicated React Native Developer from top React Native development company, HourlyDeveloper.io to design a spectacular React Native application for your business.

Consult with experts:- https://bit.ly/2A8L4vz

#hire dedicated react native developer #react native development company #react native development services #react native development #react native developer #react native

Hire Dedicated React Native Developers - WebClues Infotech

Being one of the emerging frameworks for app development the need to develop react native apps has increased over the years.

Looking for a react native developer?

Worry not! WebClues infotech offers services to Hire React Native Developers for your app development needs. We at WebClues Infotech offer a wide range of Web & Mobile App Development services based o your business or Startup requirement for Android and iOS apps.

WebClues Infotech also has a flexible method of cost calculation for hiring react native developers such as Hourly, Weekly, or Project Basis.

Want to get your app idea into reality with a react native framework?

Get in touch with us.

Hire React Native Developer Now: https://www.webcluesinfotech.com/hire-react-native-app-developer/

For inquiry: https://www.webcluesinfotech.com/contact-us/

Email: sales@webcluesinfotech.com

#hire react native developers #hire dedicated react native developers #hire react native developer #hiring a react native developer #hire freelance react native developers #hire react native developers in 1 hour

Factors affecting the cost of hiring a React Native developer in USA - TopDevelopers.co

Want to develop app using React Native? Here are the tips that will help to reduce the cost of react native app development for you.
Cost is a major factor in helping entrepreneurs take decisions about investing in developing an app and the decision to hire react native app developers in USA can prove to be fruitful in the long run. Using react native for app development ensures a wide range of benefits to your business. Understanding your business and working on the aspects to strengthen business processes through a cost-efficient mobile app will be the key to success.

#best react native development companies from the us #top react native app development companies in usa #cost of hiring a react native developer in usa #top-notch react native developer in usa #best react native developers usa #react native