Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda