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?
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}.
You can use the cancelable option that's passed to Alert. alert(title, message, buttons, {cancelable: true} .
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.
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 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 !
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',
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With