Saltar al contenido principal
Versión: 0.79

Alerta

Traducción Beta No Oficial

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

Muestra un diálogo de alerta con el título y mensaje especificados.

Opcionalmente, se puede proporcionar una lista de botones. Al pulsar cualquier botón se activará la devolución de llamada onPress correspondiente y se cerrará la alerta. Por defecto, el único botón será un botón 'Aceptar'.

Esta API funciona tanto en Android como en iOS y puede mostrar alertas estáticas. Las alertas que solicitan información al usuario solo están disponibles en iOS.

Ejemplo

iOS

En iOS puedes especificar cualquier número de botones. Cada botón puede opcionalmente definir un estilo o estar enfatizado. Las opciones disponibles están representadas por la enumeración AlertButtonStyle y el campo isPreferred en AlertButton.

Android

En Android se pueden especificar como máximo tres botones. Android tiene el concepto de botón neutral, negativo y positivo:

  • Si especificas un botón, será el 'positivo' (como 'Aceptar')

  • Dos botones significan 'negativo', 'positivo' (como 'Cancelar', 'Aceptar')

  • Tres botones significan 'neutral', 'negativo', 'positivo' (como 'Más tarde', 'Cancelar', 'Aceptar')

Las alertas en Android se pueden cerrar tocando fuera del cuadro de alerta. Esta opción está deshabilitada por defecto y se puede activar proporcionando el parámetro opcional AlertOptions con la propiedad cancelable establecida en true, es decir:
{cancelable: true}.

El evento de cancelación se puede manejar proporcionando una propiedad de devolución de llamada onDismiss dentro del parámetro options.

Example
Android


Referencia

Métodos

alert()

tsx
static alert (
title: string,
message?: string,
buttons?: AlertButton[],
options?: AlertOptions,
);

Parámetros:

NameTypeDescription
title
Required
stringThe dialog's title. Passing null or empty string will hide the title.
messagestringAn optional message that appears below the dialog's title.
buttonsAlertButton[]An optional array containing buttons configuration.
optionsAlertOptionsAn optional Alert configuration.

prompt()
iOS

tsx
static prompt: (
title: string,
message?: string,
callbackOrButtons?: ((text: string) => void) | AlertButton[],
type?: AlertType,
defaultValue?: string,
keyboardType?: string,
);

Crea y muestra un aviso para introducir texto en forma de Alerta.

Parámetros:

NameTypeDescription
title
Required
stringThe dialog's title.
messagestringAn optional message that appears above the text input.
callbackOrButtonsfunction
AlertButton[]
If passed a function, it will be called with the prompt's value
(text: string) => void, when the user taps 'OK'.
If passed an array, buttons will be configured based on the array content.
typeAlertTypeThis configures the text input.
defaultValuestringThe default text in text input.
keyboardTypestringThe keyboard type of first text field (if exists). One of TextInput keyboardTypes.
optionsAlertOptionsAn optional Alert configuration.

Definiciones de tipos

AlertButtonStyle
iOS

Un estilo de botón de alerta para iOS.

Type
enum

Constantes:

ValueDescription
'default'Default button style.
'cancel'Cancel button style.
'destructive'Destructive button style.

AlertType
iOS

Un tipo de alerta para iOS.

Type
enum

Constantes:

ValueDescription
'default'Default alert with no inputs
'plain-text'Plain text input alert
'secure-text'Secure text input alert
'login-password'Login and password alert

AlertButton

Un objeto que describe la configuración de un botón en la alerta.

Type
array of objects

Propiedades del objeto:

NameTypeDescription
textstringButton label.
onPressfunctionCallback function when button is pressed.
style
iOS
AlertButtonStyleButton style, on Android this property will be ignored.
isPreferred
iOS
booleanWhether button should be emphasized, on Android this property will be ignored.

AlertOptions

Type
object

Propiedades:

NameTypeDescription
cancelable
Android
booleanDefines if alert can be dismissed by tapping outside of the alert box.
userInterfaceStyle
iOS
stringThe interface style used for the alert, can be set to light or dark, otherwise the default system style will be used.
onDismiss
Android
functionCallback function fired when alert has been dismissed.