React Native - "type" 对 Alert 组件方法意味着什么?
React Native - what does "type" mean on Alert component method?
我是 React Native 的新手,当我阅读 Alert 组件时,我看到了 alert
方法 static alert(title, message?, buttons?, options?, type?)
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 }
)
没有关于 type
做什么的例子,有人可以告诉我那些 type
?
有没有办法在没有 Title
的情况下显示 Alert
?
看了源码发现type
是一个字符串,定义为:
type AlertType = "default" | "plain-text" | "secure-text" | "login-password";
然而,我尝试了所有这些,我没有发现任何差异。因此,我猜测了一下:可能是只针对Android,而不是iOS,但是发现Android中的Alert
定义为:
export interface AlertAndroidStatic {
alert: (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions
) => void;
}
其中 type
参数不被接受。然后,我继续看了另一个类似的react native API for iOS only called AlertIOS
,它有2个用法,第一,和Alert
API一样,你简直
AlertIOS.alert(title, message, buttons, type)
但是评论说:
@param type Deprecated, do not use
AlertIOS
的第二次使用是:
AlertIOS.prompt(title, message, buttons, type, defaultValue)
提示用户输入的地方,这里参数type
有效,如果想让用户输入一行纯文本,那么type
应该是"plain-text",如果安全文本中的一行,然后 "secure-text",如果它是登录凭据,则 type
应该是 "login-password"。请注意,对于 Android,您无法使用 Alert
提示用户输入,并且 AlertIOS
仅适用于 iOS。
结论:不知道为什么type
是Alert
API的第4个参数,对于Android,这个参数被简单地忽略了,对于 iOS,这个参数没有任何区别。我猜测是,早期react native刚出来的时候,他们没有写一个API叫AlertIOS
,而是只有Alert
。由于提示用户输入可用于 iOS 设备但不适用于 Android 设备,因此他们将 type
作为第四个参数,但如果应用程序在 运行 Android 设备。随着时间的推移,他们制作了一个名为 AlertIOS
的 API,专门用于 iOS 警报,但尚未弃用 type
参数。无论如何,只需忽略参数。
回答你的另一个问题:有没有办法显示没有标题的警报?
是的,如果您不想要标题或消息,只需将 null
传递给 API 调用即可。
export interface AlertAndroidStatic {
alert: (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions
) => void;
}
我是 React Native 的新手,当我阅读 Alert 组件时,我看到了 alert
方法 static alert(title, message?, buttons?, options?, type?)
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 }
)
没有关于 type
做什么的例子,有人可以告诉我那些 type
?
有没有办法在没有 Title
的情况下显示 Alert
?
看了源码发现type
是一个字符串,定义为:
type AlertType = "default" | "plain-text" | "secure-text" | "login-password";
然而,我尝试了所有这些,我没有发现任何差异。因此,我猜测了一下:可能是只针对Android,而不是iOS,但是发现Android中的Alert
定义为:
export interface AlertAndroidStatic {
alert: (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions
) => void;
}
其中 type
参数不被接受。然后,我继续看了另一个类似的react native API for iOS only called AlertIOS
,它有2个用法,第一,和Alert
API一样,你简直
AlertIOS.alert(title, message, buttons, type)
但是评论说:
@param type Deprecated, do not use
AlertIOS
的第二次使用是:
AlertIOS.prompt(title, message, buttons, type, defaultValue)
提示用户输入的地方,这里参数type
有效,如果想让用户输入一行纯文本,那么type
应该是"plain-text",如果安全文本中的一行,然后 "secure-text",如果它是登录凭据,则 type
应该是 "login-password"。请注意,对于 Android,您无法使用 Alert
提示用户输入,并且 AlertIOS
仅适用于 iOS。
结论:不知道为什么type
是Alert
API的第4个参数,对于Android,这个参数被简单地忽略了,对于 iOS,这个参数没有任何区别。我猜测是,早期react native刚出来的时候,他们没有写一个API叫AlertIOS
,而是只有Alert
。由于提示用户输入可用于 iOS 设备但不适用于 Android 设备,因此他们将 type
作为第四个参数,但如果应用程序在 运行 Android 设备。随着时间的推移,他们制作了一个名为 AlertIOS
的 API,专门用于 iOS 警报,但尚未弃用 type
参数。无论如何,只需忽略参数。
回答你的另一个问题:有没有办法显示没有标题的警报?
是的,如果您不想要标题或消息,只需将 null
传递给 API 调用即可。
export interface AlertAndroidStatic {
alert: (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions
) => void;
}