I'm new in React Native, and when I read about Alert component, I've seen about alert method static alert(title, message?, buttons?, options?, type?)
The example given by facebook :
Alert.alert(
//Title
'Alert Title',
//Message
'My Alert Msg',
//Button
[
{text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'OK', onPress: () => console.log('OK Pressed')},
],
//Options
{ cancelable: false }
)
and there's no example about what is type do, someone can tell me for what those type?
and is there a way to showing Alert without Title?
I read the source code and found out that type is a string defined as:
type AlertType = "default" | "plain-text" | "secure-text" | "login-password";
However, I tried all of them, I don't see any differences. Therefore, I took a guess: maybe it's for Android only, not iOS, but found out that Alert in Android is defined as:
export interface AlertAndroidStatic {
alert: (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions
) => void;
}
where type param is not accepted. Then, I continued to read another similar react native API for iOS only called AlertIOS, which has 2 usages, first, same as Alert API, you simply
AlertIOS.alert(title, message, buttons, type)
But the comment says:
@param type Deprecated, do not use
Second usage of AlertIOS is:
AlertIOS.prompt(title, message, buttons, type, defaultValue)
where you prompt for user input, here, param type has effect, if you want user to enter one line in plain text, then type should be "plain-text", if one line in secure text, then "secure-text", if it's login credential, then type should be "login-password". Note, for Android, you can't prompt for user input using Alert, and AlertIOS is only available for iOS.
Conclusion: I do not know why type is the 4th parameter of Alert API, for Android, this param is simply ignored, and for iOS, this param doesn't make any difference. My guess is, in the early days when react native just came out, they didn't write an API called AlertIOS, instead, they only have Alert. Since prompting for user input is available for iOS devices but not for Android devices, they put type as the 4th param, but ignore it if the app is running on an Android device. And as time goes by, they made an API called AlertIOS, specifically for iOS alerts, but haven't deprecated type param yet. Anyway, simply ignore the param.
To answer your other question: Is there a way to show Alert without title?
Yes, if you don't want a title OR message, simply pass null to the API call.
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