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?
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?
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
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',
}
});
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 !
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.
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!
{ cancelable: false } this option is only alive in android, not in ios
You need to use custom alert for this.
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
.
© 2022 - 2025 — McMap. All rights reserved.