¿Alguien ha trabajado con la función de detección de clics de vista web? Tener una vista web cargando un sitio donde necesito detectar el clic del usuario en un botón. Intenté usar javascript inyectado pero no pude lograr el resultado deseado. Aquí está mi código
<WebView scalesPageToFit startInLoadingState originWhitelist={['*']} style={{ flex: 1 }} source={{ uri: url }} javaScriptEnabled={true} domStorageEnabled={true} setSupportMultipleWindows={false} />¿Cómo agregaremos soporte para el evento de clic? ¿Necesitamos cambiar el backend o podemos manejarlo en el front-end?
Resolví el problema con el siguiente código. Esto funciona para mí y detecta clic dentro de html:
import { WebView } from 'react-native-webview'; export default function App() { const html = ` <html> <body style='color:red'> This is a test <input type="button" value="Click me" onclick="msg()"> <script> function msg() { setTimeout(function () { window.ReactNativeWebView.postMessage("Hello!") }, 2000) } </script> </body> </html> `; return ( <View style={styles.container}> <StatusBar style="auto" /> <WebView source={{ html }} style={{width:400,height:200,backgroundColor:'white',marginTop:20}} onMessage={event => { alert(event.nativeEvent.data); }} /> </View> ); }