Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss Alert programmatically react native

For 2 kind of errors I want to show 2 different alert

I want to remove any alert already present, before showing up the next alert.

Right now one alert comes above the previous alert,

How can I dismiss on alert before showing up the next one?

like image 500
Aishwarya Avatar asked Dec 07 '18 09:12

Aishwarya


People also ask

How do I dismiss alert react native?

React native alert in Android On Android, the by default alerts can be dismissed by clicking outside the alert box. This event can be handled by providing an optional parameter, with an onDismiss call back property { onDismiss: () => {} }. Alternatively, this can be carried out using the property {cancelable: false}.

How do I close alert dialog in react?

You can use the cancelable option that's passed to Alert. alert(title, message, buttons, {cancelable: true} .

How do I show alert dialog in react native?

First, we have to import Alert API to show an Alert message with one, two, and three buttons. Next, we define Alert. alert() function inside the react-native App Component. These Alert popups show alert messages with one, two, and three buttons.


3 Answers

You can't dismiss the native Alert programmatically, you can use custom alert box or a Modal component which have a "visible" props : https://facebook.github.io/react-native/docs/modal

like image 112
Lïet Avatar answered Oct 09 '22 02:10

Lïet


To dismiss a react native alert simply do it

Alert.alert(
  'No Internet !',
  'Your internet does not seems to work',
  [
    { text: "Try again", onPress: () =>this.myfunction()},
    { text: "Dismiss", onPress: () =>console.log('dismissing alert')}
  ], 
  { cancelable: false }
)

Chill Pill !

like image 40
Ali Akram Avatar answered Oct 09 '22 03:10

Ali Akram


To handle this situation, the best solution is to design custom alert by using  **react-native-dialog**. here is running sample code, it works fine for me and you can give your custom color and style in this alert
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native'; 
// import PropTypes from 'prop-types';
// import Dialog from "react-native-dialog";
 import Alertfunction from './src/CustomAlert'




 export default class App extends Component{

    render() {

      return (

        <Alertfunction Title={"Alert"} FontSize = {30} FontColor= '#FF9800'  Visible={true}/>


      );
    }
  }


const styles = StyleSheet.create({

 MainContainer :{

  flex:1,
  paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
  alignItems: 'center',
  justifyContent: 'center',

  }

});



CustomAlert.js

import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native'; 
import PropTypes from 'prop-types';
import Dialog from "react-native-dialog";

class Alertfunction extends Component {
    state = {
        dialogVisible: this.props.Visible
      };

      showDialog = () => {
        this.setState({ dialogVisible: this.props.Visible });
      };

      handleCancel = () => {
         this.setState({ dialogVisible: false });
        // this.props.Visible=false;
      };

      handleDelete = () => {
         this.setState({ dialogVisible: false });
        //this.props.Visible=false;
      };

  render() {

    return (

            <View>
                <TouchableOpacity onPress={this.showDialog}>
                <Text >{this.props.Title}</Text>
                </TouchableOpacity>
                <Dialog.Container visible={this.state.dialogVisible}>
                <Dialog.Title style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>{this.props.Title}</Dialog.Title>
                <Dialog.Description style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>
                    Do you want to delete this account? You cannot undo this action.
                </Dialog.Description>
                <Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="Cancel" onPress={this.handleCancel} />
                <Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="ok" onPress={this.handleDelete} />
                </Dialog.Container>
            </View>

    );
  }
 }

export default Alertfunction;
Alertfunction.propTypes =
{
    Title: PropTypes.string,
  FontSize: PropTypes.number,
  FontColor: PropTypes.string,
  Visible: PropTypes.bool,

}

Alertfunction.defaultProps =
{
    Title: "Default Name",
  FontColor: "#00E676",
  FontSize: 15,
  Visible:false
}

const styles = StyleSheet.create({

 MainContainer :{

  flex:1,
  paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
  alignItems: 'center',
  justifyContent: 'center',

  }

});
like image 30
Asad Avatar answered Oct 09 '22 02:10

Asad