Detect button click from WebView in React Native

后端 未结 2 1181
广开言路
广开言路 2021-01-07 05:30

Can anyone please help me on how to detect Webview button click event in React Native? As shown in the code below, I have a Button in my WebView (index.html) for which i wan

2条回答
  •  时光说笑
    2021-01-07 06:23

    Add window.postMessage() to your page file and onMessage={this.onMessage} to your React Native WebView component.

    Example:

    // index.html (Web)
    ...
    
    ...
    
    
    // MyWebView.js (React Native)
    
    ...
    
      
    onMessage(data) {
      //Prints out data that was passed.
      console.log(data);
    }
    

    Edit: Working example:

    App.js

    onMessage(m) {
     alert(m.nativeEvent.data);
    }
    render() {
      return(
         this.onMessage(m)} 
        />
      );
    }
    

    index.html

    
    
    
    

    Hope it helps

提交回复
热议问题