Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Web view click detection

has anyone worked with web view click detection feature? Have a web view loading a site where I need to detect the click by user on a button. I tried using injected javascript but couldn't achieve the desired result. Here is my code

<WebView scalesPageToFit
    startInLoadingState
    originWhitelist={['*']}
    style={{ flex: 1 }} source={{ uri: url  }}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    setSupportMultipleWindows={false}
    /> 

How will we add support for click event ? Do we need to change backend or we can handle on front end ?

like image 818
Preetika Avatar asked Sep 05 '26 09:09

Preetika


1 Answers

I solved the issue with below code. This is working for me and detecting click inside 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>
);
}


like image 57
Preetika Avatar answered Sep 06 '26 22:09

Preetika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!