Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How to pass variable to React Native's Alert.alert button's onPress?

I'm trying to pass an object/variable to Alert.alert() function's button onPress. Unfortunately I get "undefiend" as result. Here is my code example;

console.log("HERE ITEM SHOWS THE CORRECT VALUES", item);

Alert.alert(
    "Warning",
    "Test message",
    [
        {
            text: "Cancel",
            onPress: () => console.log("Cancel Pressed"),
            style: "cancel"
        },
        {
            text: "Start",
            onPress: () => { 
                    console.log("-----> HERE I CANNOT GET THE VALUES FOR ITEM", item);
            }
        }
    ]
    

);

How can I pass the values? Thank you very much for your help :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is a working example: https://snack.expo.dev/@gavindmello97/moody-raspberries

<TouchableOpacity onPress = {() => {
    var tempVarForTest = "StackOverflow"
    Alert.alert(
      "Warning",
      "Test message",
      [
          {
              text: "Cancel",
              onPress: () => console.log("Cancel Pressed"),
              style: "cancel"
          },
          {
              text: "Start",
              onPress: () => { 
                      alert("-----> HERE I CANNOT GET THE VALUES FOR ITEM "+ tempVarForTest);
              }
          }
      ]
      

  );
}}>
    <Text style={styles.paragraph}>
      Click me to open alertBox
    </Text>
  </TouchableOpacity>

I have used the same code as in your question. Do check if I missed anything. Since it reads the variable, it can also read an object, text, etc.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda