Home  >  Article  >  Web Front-end  >  About communication between react native and webview

About communication between react native and webview

小云云
小云云Original
2018-02-01 13:17:482092browse

WebView is a component in ReactNative. It can create a native WebView and can be used to access a web page. This article mainly introduces the sample code for communication between react native and webview. The editor thinks it is quite good, so I will share it with you now. , also as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Sometimes we need to communicate between RN and WebView, or transfer data, or send message notifications. At this time, we need to use the following knowledge.

1: WebView sends data to RN:

First, we build a webview:


##

 {
    this.handleMessage(e)
  }}
  
/>

You can see that there is an onMessage method,


onMessage function

The function corresponding to this attribute can be triggered when the window.postMessage method is called in the webpage inside the webview, thus Realize data exchange between web page and RN. When setting this property, a postMessage global function will be injected into the webview and override the implementation of the same name that may already exist.

Window.postMessage on the web page only sends one parameter data, which is encapsulated in the event object on the RN side, namely event.nativeEvent.data. data can only be a string.

It can be seen that this method is used to receive data from Webview (that is, html).


The internal implementation is to process the received data:



handleMessage(e) {
  this.setState({webViewData: e.nativeEvent.data});
}

e.nativeEvent.data is the data sent from inside the webview.


Just doing this is not enough, this is just For Rn side processing, we also need to write a method of sending data in html:



var data = 0;

function sendData(data) {
  if (window.originalPostMessage) {
    window.postMessage(data);
  } else {
    throw Error('postMessage接口还未注入');
  }
}
document.getElementById('button').onclick = function () {
  data += 100;
  sendData(data);
}

window.postMessage is to send data to RN.

Two: RN sends data to Webview:

First define a method to send data:



sendMessage() {
  this.refs.webview.postMessage(++this.data);
}

This step is very simple.

Just write the data you want to send as a parameter in this method.

Then, there must also be a corresponding method for receiving data in the html:



window.onload = function () {
  document.addEventListener('message', function (e) {
    document.getElementById('data').textContent = e.data;
  });

}

This enables communication between Rn and Webview.


Put the source code afterwards:






  
  
  

收到react native发送的数据:

web.js:


##

/**
 * Created by 卓原 on 2017/8/17.
 * zhuoyuan93@gmail.com
 */
import React from 'react';
import {
  View,
  Text,
  StyleSheet,
  WebView
} from 'react-native';

export default class Web extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      webViewData: ''
    }
    this.data = 0;
  }

  sendMessage() {
    this.refs.webview.postMessage(++this.data);
  }

  handleMessage(e) {
    this.setState({webViewData: e.nativeEvent.data});
  }

  render() {
    return (
      
        
           {
              this.handleMessage(e)
            }}

          />

        
        来自webview的数据 : {this.state.webViewData}
         {
          this.sendMessage()
        }}>发送数据到WebView
      
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 22,
    backgroundColor: '#F5FCFF',
  },

});

Related recommendations:


JS Interactively click on the picture in WKWebView and preview the example

Detailed explanation of WebView knowledge points

Detailed explanation of the method of loading HTML code using WebView

The above is the detailed content of About communication between react native and webview. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn