Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display toast message in react native

Tags:

I am trying to display a toast message in react native on button click

import React,{ Component } from 'react';
import { StyleSheet,TextInput, View, Button, Text, ToastAndroid } from 'react-native';

export default class App extends Component {

  state = {
              placeName : "",
              titleText: "Text view"
          }

  placeNameChangeHandler = val =>{
   this.setState({
     placeName : val
   })
  }

  placeSubmitHandler = () =>{
    this.setState({
      titleText: this.state.placeName.trim() 

    })
  }

   render() {
      return (
      <View style={styles.rootContainer}>

        <View style={styles.btnEditContainer}>
          <View style ={styles.wrapStyle}>
          <TextInput
          style = {styles.textInputStyle}
          value = {this.state.placeName}
          onChangeText = {this.placeNameChangeHandler}
          />
        </View>
          <View style ={styles.wrapStyle}>
          <Button
          title="Add"
          style ={styles.buttonStyle}
          onPress ={this.placeSubmitHandler}/>
        </View>
        </View>

        <View style={styles.textContainer}>
          <View style ={styles.wrapStyle}>
            <Text
            style ={styles.textStyle}>
            {this.state.titleText}
            </Text>
          </View>
        </View>

        </View>
      );
   }
}

const styles = StyleSheet.create({
  rootContainer: {
    height:"100%",
    width:"100%",
    backgroundColor: "#008000",
    flexDirection:"column",
    alignItems:"center",
    justifyContent: "center"
  },
  btnEditContainer: {
    backgroundColor:"#008080",
    flexDirection:"row",
    alignItems:"center",
    justifyContent: "center"
  },
  textContainer: {
    backgroundColor:"#00FFFF",
    flexDirection:"column",
    alignItems:"center",
    justifyContent: "center"
  },
  textStyle: {
    fontSize: 20,
    flexDirection:"column",
    alignItems:"center",
    justifyContent: "center"
  },
  buttonStyle: {
  },
  textInputStyle: {
    borderColor:"black",
    borderWidth:1,
  },
  wrapStyle: { marginLeft:5,
    marginRight:5 },
});
like image 775
Devrath Avatar asked Aug 23 '19 10:08

Devrath


People also ask

How do you show a toast message in React Native?

To use react-native-root-toast , you have to install the module from npm with npm install react-native-root-toast . Next, you must wrap the root component of your app with <RootSiblingParent> to allow toasts in any part of your app.

How do you show a toast message?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.

How do you use React Native simple toast?

The Toast will be presented in that VC. For Example, let's say you're showing a ReactNative. Modal in your app - in that case, the presented VC is a RCTModalHostViewController . If you present a Toast while that Modal is shown, and then hide the Modal , the Toast will disappear together with the Modal .


2 Answers

Can use below notifyMessage to show toast message:

import {
      ToastAndroid,
      Platform,
      AlertIOS,
    } from 'react-native';

function notifyMessage(msg: string) {
  if (Platform.OS === 'android') {
    ToastAndroid.show(msg, ToastAndroid.SHORT)
  } else {
    AlertIOS.alert(msg);
  }
}

OR

Use react-native-simple-toast in both iOS & Android.

import Toast from 'react-native-simple-toast';

Toast.show('This is a toast.');
Toast.show('This is a long toast.', Toast.LONG);
like image 139
Rocky Avatar answered Sep 20 '22 00:09

Rocky


React Native now has built-in API Alert which works both on IOS and Android. https://reactnative.dev/docs/alert

like image 40
sminmgr Avatar answered Sep 19 '22 00:09

sminmgr