Dismiss Alert programmatically react native
Asked Answered
L

7

7

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?

Lagos answered 7/12, 2018 at 9:40 Comment(0)
U
2

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

Upcast answered 7/12, 2018 at 10:21 Comment(0)
A
2
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',

  }

});
Apologete answered 5/12, 2019 at 10:33 Comment(0)
F
2

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 !

Fibroblast answered 8/11, 2020 at 10:30 Comment(0)
V
0

You can keep a boolean check "isAlertVisible" to deal with this situation. Mark "isAlertVisible" true whenever an Alert is triggered and add a check to every Alert before opening it, that whether "isAlertVisible" is true or not.

Vendor answered 7/12, 2018 at 11:45 Comment(0)
F
0

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

Moreover, you can pass a function onDismiss, so that you know that the user didn't choose anything!

Hope this helped!

Fell answered 25/1, 2022 at 8:34 Comment(0)
W
0

{ cancelable: false } this option is only alive in android, not in ios

You need to use custom alert for this.

Work answered 31/7, 2023 at 22:15 Comment(0)
T
-4

Actually you can!

For example:

Alert.alert(
  'Oops!',
  'The provided passwords did not match',
  [
    { text: "Try again", onPress: () => null}
  ], 
  { cancelable: false }
)

In this case, presseing Try again will close the Alert.

Taub answered 15/7, 2019 at 19:12 Comment(2)
Without pressing anything on alert i want it to happenLagos
well thats kinda obvious, since thats how alert works. this is still a good solution, despite the downvotes. :)Taub

© 2022 - 2025 — McMap. All rights reserved.