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 :)
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.