Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!