Javatpoint Logo
Javatpoint Logo

React Native Toast

What is a Toast?

Toasts are the technique in mobile development to notify the users about interrupting what they're doing.

According to the Android Developer Documentation: "A toast provides the feedback of the operation in a small pop-up window. It fills the space required for the message, and the activity is visible and interactive. Toasts automatically disappear afterward." of a waiting time".

React Native's Toast Android API exposes the Android platform's ToastAndroid module in the JS module.

It provides the thing (message, duration) method, which takes the below parameters:

  • Message: It is a string with the text to toast
  • Duration: The duration of Toast is either the
    ToastAndroid.SHORT or ToastAndroid.LONG.

Alternatively, we can use the display with severity (message, duration, severity) to specify where the TToastappears on the screen layout. Maybe the ToastAndroid.TOP, ToastAndroid.BOTTOM, or ToastAndroid.CENTER.

The 'showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset)' adds the ability that specify an offset in the pixels.

Imperative hack

The ToastAndroid API is a must, but there is a way to expose a declarative component like in this example:

Methods

  • show()
  • staticshow(message, duration)
  • showGravity()
  • static show with gravity (message, duration, gravity)
  • displayWithGravityAndOffset()
  • static show with gravity and offset (message, duration, gravity, x-offset, y-offset)

Properties

SHORT

It indicates the duration on the screen.

LENGTH

It indicates the duration of the screen.

UP

It indicates the position of the screen.

DOWN

It indicates the position of the screen.

CENTER

It indicates the position of the screen.

How to display a toast popuppopup?

We have two solutions: an API from the react-native package and a library maintained by the React Native community to present a toast.

Android only: ToastAndroid

Toasts are a native Android feature, but iOS doesn't have them by default. If we need Toaston Android, we can use the ToastAndroid API provided by React Native.

Uses

Import ToastAndroid from the 'react-native' package and call ToastAndroid.show with a message and duration option to display a toast with the help of ToastAndroid:

The upper code results in the on a Pixel below:

React Native Toast

There are many other ways to set the toast position, duration, and severity options.

Cross-platform: react-native-root-toast

Uses

To use react-native-root-toast, you should install the module from npm with npm install react-native-root-toast.

Then we need to wrap the root component of the app with <RootSiblingParent> to allow the sibling in your app.

Then anywhere in the app, import Toast from react-native-root-toast and call Toastshow and Toasthide to manage the Toast on the screen.

React-native-root-toast also has a component API if you want to manage the toasts declaratively.

An animated toast message component for React Native

An animated toast message component used for React Native to be called imperatively.

Install

Use

Then use it in the app:

We can use the following options to suit the needs. Everything is optional unless specified when calling the show method.

The use of | below is that one of the displayed values be used. If only one value is displayed, then it is the default value.

Customization of the types of toast

You can add a configuration prop rendering the Toast at the root of your app If you want to add custom types or override existing ones.

So use the library like before:

Output details:

React Native Toast

FAQ’s

1. How to display the Toastinside a Modal?

How are referees tracked?

When you render an instance of <Toast /> at the entry point (root) of your application, a reference is created, and internal tracking is performed.

Under the hood, this reference is used when you imperatively call TToast show() or Toast. disguise().

Showing a toast inside a modal

Things get different when you have a Modal. The Modal component sits at the top of the React root view, so the way to display something on top of the Modal is to read it inside the Modal.

This means you need a new <Toast /> instance rendered inside your Modal (in addition to keeping the existing <Toast /> instance outside, at your application's entry point).

Everything else works as usual; we show and hide Toasts using the imperative API: Toast. show() or Toast. disguise().

When the Modal is visible, the reference within the Modal will be used; otherwise, the one outside.

The reference is automatically tracked; whichever instance of <Toast /> last had its reference set will be used when shown/hidden.

Notes on react-native-modal or NativeStackNavigator

The same requirements as above will apply when using react-native-modal or a NativeStackNavigator with presentation: 'modal':

2. How to render the Toast when using a navigation library?

Usage with react-navigation

To make the Toastvisible at the top of the navigation view hierarchy, render it as the last child in the view hierarchy (along with the root navigation component):

3. How to mock library to prank test?


Next TopicReact Native Sound





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA