The problem encountered by react-native webview is replaced by react-native-webview

tags: react-native-webview  react-native-webview interact with H5

Briefly introduce the problems encountered in the process of replacing react-native-webview with the webview that comes with react-native.

Based on version:

 "react-native": "0.60.6",
 "react-native-webview": "^7.4.2",

1. Installation and use methods are omitted, you can seereact-native-webviewThis document.

2. React-native-webview interacts with h5.

Write the main code in react-native-webview webview below:

import React, { Component } from 'react';
import { WebView } from 'react-native-webview';

/**
   * onMessage receives HTML5 messages sent to RN Webview
 *
 */

onMessage = (event) => {
    
    let data = JSON.parse(event.nativeEvent.data);
    
         console.log ("Received message from html5", data);
}


/**
   * RN Webview sends a message to HTML5 
 * data  object
 *
   * Trigger the function by calling
 */

postMessageToH5(data){
    
   this.webView.injectJavaScript(`
       (function(){
          window.postMessage(${JSON.stringify(data)},'*');
       })();
       true;
   `)

}


class MyWeb extends Component {
  render() {
    return (
      <WebView
        source={{ uri: 'https://infinite.red' }}
        style={{ marginTop: 20 }}
        ref={webView => this.webView = webView}
        onMessage={this.onMessage}
      />
    );
  }
}

Next, write the main code part in HTML5:

<script>
    /**
     * Send message to RN webview
     */
    function onClick() {
        let postData = {
            type: 1,
                         title: "Test"
        }
                 // Send a message 
        if(window.ReactNativeWebView){
            window.ReactNativeWebView.postMessage(JSON.stringify(postData))
        }
    }
    /**
           * Monitor messages sent by RN webview to html5   
     */
    window.onload = function(){
        window.addEventListener("message",function(msg){
            let data = JSON.stringify(msg.data);
                         console.log ("Receive messages from RN", data);
        })
    }
    
</script>

The above is the interaction of react-native-webview and HTML5. It is slightly different from the interaction between the Webview and html5 in the previous version of react-native. A

 

3. The originWhitelist attribute in react-native-webview is used to put the original

originWhitelist={['*']}

Replace with

originWhitelist={["https://*", "http://*", "file://*", "sms://*", "tel://*"]}

This method can solve some link errors. For example, the problem of net :: ERR_UNKNOWN_URL_SCHEME.

Intelligent Recommendation

React native webview communicates with H5

rn encapsulates native jsbridge to interact with H5 rn webview problem I wrote an article that encapsulates native webview before,rn encapsulates native jsbridge to interact with H5 But that’s o...

React Native development-component WebView

Preface When developing Android, we generally have some requirements to load web pages or execute some JavaScript. We all know that the control that implements this function in Android is WebView. The...

React Native learning eight-WebView

creates a native WebView that can be used to visit a web page. Screenshot Attributes iosallowsInlineMediaPlayback bool # Specify whether the HTML5 video is played at the current position of the w...

React Native WebView optimization (iOS)

problem Opening a WebView in React Native will have a very obvious white screen time.   analyse problem I read some optimizations about WebView on the Internet, refer to the following WebView ope...

The interaction between React Native and WebView

1. Inject JavaScript into WebView When using injectJavaScript to inject javascript in webview, the method is as follows When injecting javascript strings through injectedJavaScript, only static text c...

More Recommendation

React-Native WebView control usage

Like Android and IOS, there is also a control WebView that loads webpage data in React Native. Common methods are: source {uri: string, method: string, headers: object, body: string}, {html: string, b...

Introduction to React Native WebView plugin

In React Native, WebViews can allow mobile apps to access any web portal. In other words, the web view allows us to open a web URL in the app interface. Although React Native provides a built-in web v...

React-Native-WebView and H5 Communication

H5 sends data Window.reactnativeWebView.PostMessage ('event type type'); RN receives data (This.WebView is a react-native-webview instance by refs) Way, this.webview.injectjavascript ('H5 function nam...

Reactnative uses React-Native-Webview.

Reactnative uses React-Native-Webview. question Initial useReactNativeWhen the original Demo was used, it could not be loaded when using the `React-Native-WebView. Looking at the answer from foreign c...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top