Estoy tratando de pasar un objeto/variable al botón de la función Alert.alert() onPress . Desafortunadamente, obtengo "undefiend" como resultado. Aquí está mi ejemplo de código;
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); } } ] );¿Cómo puedo pasar los valores? Muchas gracias por su ayuda :)
Aquí hay un ejemplo práctico: 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>He usado el mismo código que en tu pregunta. Comprueba si me perdí algo. Como lee la variable, también puede leer un objeto, texto, etc.