Example of Popup Dialog in React Native

React Native Popup Dialog

Here is an Example of Popup Dialog in React Native. We will use PopupDialog component provided by react-native-popup-dialog to make a Dialog.

We can use Alert instead of the popup dialog if we just want to show the text but when it comes to the customization of alert content we have to use the popup dialog.

How to use Popup Dialog Component?

<Dialog
  onDismiss={() => {
    setDefaultAnimationDialog(false);
  }}
  width={0.9}
  visible={defaultAnimationDialog}
  rounded
  actionsBordered
  dialogTitle={
    <DialogTitle
      title="Default Animation Dialog Simple"
      style={{
        backgroundColor: '#F7F7F8',
      }}
      hasTitleBar={false}
      align="left"
    />
  }
  footer={
    <DialogFooter>
      <DialogButton
        text="CANCEL"
        bordered
        onPress={() => {
          setDefaultAnimationDialog(false);
        }}
        key="button-1"
      />
      <DialogButton
        text="OK"
        bordered
        onPress={() => {
          setDefaultAnimationDialog(false);
        }}
        key="button-2"
      />
    </DialogFooter>
  }>
  <DialogContent
    style={{
      backgroundColor: '#F7F7F8',
    }}>
    <Text>
      Here is an example of default animation dialog
    </Text>
  </DialogContent>
</Dialog>

Other components provided by Popup dialog

Popup dialog also provides the following components:

  1. DialogTitle
  2. DialogButton
  3. SlideAnimation
  4. ScaleAnimation
  5. FadeAnimation

This example contains three types of popup dialogs:

  1. Default Animation Dialog
  2. Scale Animation Dialog
  3. Slide Animation Dialog

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use PopupDialog we have to install react-native-popup-dialog package.

To install this open the terminal and jump into your project

cd ProjectName

Run the following command

npm install react-native-popup-dialog --save

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update the react-native-popup-dialog dependency in your package.json file.

Code for React Native Popup Dialog

Now Open App.js in any code editor and replace the code with the following code

App.js

// Example of Popup Dialog in React Native
// https://aboutreact.com/popup-dialog/

// import React in our code
import React, {useState} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  View,
  Text,
  TouchableHighlight,
  StyleSheet,
  Button,
} from 'react-native';

import Dialog, {
  DialogTitle,
  DialogContent,
  DialogFooter,
  DialogButton,
  SlideAnimation,
  ScaleAnimation,
} from 'react-native-popup-dialog';

const App = () => {
  const [
    defaultAnimationDialog, setDefaultAnimationDialog
  ] = useState(false);
  const [
    scaleAnimationDialog, setScaleAnimationDialog
  ] = useState(false);
  const [
    slideAnimationDialog, setSlideAnimationDialog
  ] = useState(false);

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
          React Native Toast – Toast Alert for Android
        </Text>
        {/* For Default Animation Dialog */}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={() => setDefaultAnimationDialog(true)}>
          <Text style={styles.buttonTextStyle}>
            Default Animation Dialog
          </Text>
        </TouchableHighlight>

        {/* For Scale Animation Dialog */}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={() => setScaleAnimationDialog(true)}>
          <Text style={styles.buttonTextStyle}>
            Scale Animation Dialog
          </Text>
        </TouchableHighlight>

        {/* For Slide Animation Dialog */}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={() => setSlideAnimationDialog(true)}>
          <Text style={styles.buttonTextStyle}>
            Slide Animation Dialog
          </Text>
        </TouchableHighlight>

        <Dialog
          onDismiss={() => {
            setDefaultAnimationDialog(false);
          }}
          width={0.9}
          visible={defaultAnimationDialog}
          rounded
          actionsBordered
          dialogTitle={
            <DialogTitle
              title="Default Animation Dialog Simple"
              style={{
                backgroundColor: '#F7F7F8',
              }}
              hasTitleBar={false}
              align="left"
            />
          }
          footer={
            <DialogFooter>
              <DialogButton
                text="CANCEL"
                bordered
                onPress={() => {
                  setDefaultAnimationDialog(false);
                }}
                key="button-1"
              />
              <DialogButton
                text="OK"
                bordered
                onPress={() => {
                  setDefaultAnimationDialog(false);
                }}
                key="button-2"
              />
            </DialogFooter>
          }>
          <DialogContent
            style={{
              backgroundColor: '#F7F7F8',
            }}>
            <Text>
              Here is an example of default animation dialog
            </Text>
          </DialogContent>
        </Dialog>

        <Dialog
          onTouchOutside={() => {
            setScaleAnimationDialog(false);
          }}
          width={0.9}
          visible={scaleAnimationDialog}
          dialogAnimation={new ScaleAnimation()}
          onHardwareBackPress={() => {
            setScaleAnimationDialog(false);
            console.log('onHardwareBackPress');
            return true;
          }}
          dialogTitle={
            <DialogTitle
              title="Scale Animation Dialog Sample"
              hasTitleBar={false}
            />
          }
          actions={[
            <DialogButton
              text="DISMISS"
              onPress={() => {
                setScaleAnimationDialog(false);
              }}
              key="button-1"
            />,
          ]}>
          <DialogContent>
            <View>
              <Text>
                Here is an example of scale animation dialog.
                Close using back button press
              </Text>
              <Button
                title="Close"
                onPress={() => {
                  setScaleAnimationDialog(false);
                }}
                key="button-1"
              />
            </View>
          </DialogContent>
        </Dialog>

        <Dialog
          onDismiss={() => {
            setSlideAnimationDialog(false);
          }}
          onTouchOutside={() => {
            setSlideAnimationDialog(false);
          }}
          visible={slideAnimationDialog}
          dialogTitle={
            <DialogTitle
              title="Slide Animation Dialog Sample"
            />
          }
          dialogAnimation={
            new SlideAnimation({slideFrom: 'bottom'})
          }>
          <DialogContent>
            <Text>
              Here is an example of slide animation dialog.
              Please click outside to close the the dialog.
            </Text>
          </DialogContent>
        </Dialog>
      </View>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#307ecc',
    padding: 16,
  },
  buttonStyle: {
    minWidth: '100%',
    padding: 10,
    backgroundColor: '#f5821f',
    margin: 15,
  },
  buttonTextStyle: {
    color: 'white',
    textAlign: 'center',
  },
  titleStyle: {
    color: 'white',
    textAlign: 'center',
    fontSize: 20,
    marginTop: 10,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

      

Output in Online Emulator

This is how you can make a popup dialog in React Native. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

7 thoughts on “Example of Popup Dialog in React Native”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.