No puedo obtener ningún resultado de mi función test() que he escrito en Boxes.js cuando agrego onPress={test} a mi etiqueta View en Dice.js He intentado agregar test() en etiquetas como Text , luego funciona, pero no cuando lo pongo en la etiqueta Ver.
Cajas.js:
import react from "react"; import { StyleSheet, Text, View, Image } from "react-native"; import Dice from "./Dice"; import PatternDivider from "./PatternDivider"; export function test() { console.log("hi"); } export default class Boxes extends react.Component { render() { return ( <View style={styles.box}> <Text style={styles.header}>Advice #117</Text> <Text style={styles.advice}> It is easy to sit up and take notice, what's difficult is getting uu and taking action </Text> <PatternDivider /> <Dice /> </View> ); } }Dice.js:
import react from "react"; import { StyleSheet, Text, View, Image } from "react-native"; import { test } from "./Boxes"; export default class Dice extends react.Component { render() { return ( <View style={styles.circle} onPress={test}> <Image source={require("../assets/icon-dice.png")} style={styles.dice} /> </View> ); } }Si desea que se pueda hacer clic en Dice , use uno de los componentes que maneja eventos de prensa, como Pressable . No todos los componentes aceptan una propiedad onPress , View es uno de ellos.
import react from "react"; import { StyleSheet, Text, View, Image, Pressable } from "react-native"; import { test } from "./Boxes"; export default class Dice extends react.Component { render() { return ( <Pressable style={styles.circle} onPress={test}> <Image source={require("../assets/icon-dice.png")} style={styles.dice} /> </Pressable> ); } }El componente View de React Native no tiene una propiedad onPress https://reactnative.dev/docs/view