React Native SnackBar – Bottom Flash Message

React Native SnackBar

Here is an example of React Native SnackBar. We will use react-native-snackbar-component to generate Snackbar in React Native.

React Native SnackBar is a completely new way to show alerts to the user. While using an Alert dialog we block the user’s visible area and force him to click on the screen to continue but in the case of SnackBar, we show a small strip below the screen by adjusting the view. it comes up from the bottom (we can also remove it after a certain time). It also has an action text which will help you to perform some actions. For example, you have seen Gmail App which notifies you after removing the Emails and gives you an action button also to revert your action.

How to use SnackBar Component?

<Snackbar
    visible={snackIsVisible}
    //SnackBar visibility control
    textMessage="This is Snackbar"
    //Text on SnackBar
    actionHandler={() => {
      //function called while clicking on action Text
      alert("let's go");
      //After handling click making nackBar invisible
      setSnackIsVisible(false);
    }}
    actionText="let's go"
    //action Text to print on SnackBar
    distanceCallback={distance => {
      //Number indicating distance taken up by snackbar
      setDistance(distance);
    }}
/>

In this example, we will show the Snackbar with a click of a button and will generate an alert while clicking on the action button of the Snackbar. So let’s get started.

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 Snackbar in our project, we have to import react-native-snackbar-component library.

To install the library open the terminal and jump into your project

cd ProjectName

Now run the following command

npm install react-native-snackbar-component --save

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module directory named react-native-snackbar-component.

–save is optional, it is just to update the react-native-snackbar-component dependency in your package.json file.

Code for React Native SnackBar

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

App.js

// React Native SnackBar – Bottom Flash Message
// https://aboutreact.com/react-native-snackbar/

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

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

//import Snackbar
import Snackbar from 'react-native-snackbar-component';

const App = () => {
  const [snackIsVisible, setSnackIsVisible] = useState(false);
  const [distance, setDistance] = useState(0);

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
          Example of Tooltip in React Native for Android and IOS
        </Text>
        <Text style={styles.titleStyle}>
          www.aboutreact.com
        </Text>
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={() => setSnackIsVisible(true)}>
          <Text style={styles.buttonTextStyle}>
            Show Snackbar
          </Text>
        </TouchableHighlight>
        <Snackbar
          visible={snackIsVisible}
          //SnackBar visibility control
          textMessage="This is Snackbar"
          //Text on SnackBar
          actionHandler={() => {
            //function called while clicking on action Text
            alert("let's go");
            //After handling click making nackBar invisible
            setSnackIsVisible(false);
          }}
          actionText="Action Button"
          //action Text to print on SnackBar
          distanceCallback={(distance) => {
            //Number indicating distance taken up by snackbar
            setDistance(distance);
          }}
        />
      </View>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#307ecc',
    padding: 16,
  },
  buttonStyle: {
    width: '100%',
    height: 40,
    padding: 10,
    backgroundColor: '#f5821f',
    marginTop: 30,
  },
  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

That was the React Native SnackBar. 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. 🙂

4 thoughts on “React Native SnackBar – Bottom Flash Message”

  1. Thanks for this post. Could you tell me if it is possible to show the snackbar between screens, For example: I am on connect people screen, I push on connect button and show the snack bar with 30 seconds. Then and before the time expires, I go my profile info screen. Can continue showing snack bar over the second screen?. Thanks in advance…

    Reply

Leave a Comment

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