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:
##
<WebView ref={'webview'} source={require('./index.html')} style={{width: 375, height: 220}} onMessage={(e) => { this.handleMessage(e) }} />
handleMessage(e) { this.setState({webViewData: e.nativeEvent.data}); }
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); }
Two: RN sends data to Webview:
sendMessage() { this.refs.webview.postMessage(++this.data); }
Just write the data you want to send as a parameter in this method.
window.onload = function () { document.addEventListener('message', function (e) { document.getElementById('data').textContent = e.data; }); }
<!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <p style="text-align: center"> <button id="button">发送数据到react native</button> <p style="text-align: center">收到react native发送的数据: <span id="data"></span></p> </p> <script> var data = 0; function sendData(data) { if (window.originalPostMessage) { window.postMessage(data); } else { throw Error('postMessage接口还未注入'); } } window.onload = function () { document.addEventListener('message', function (e) { document.getElementById('data').textContent = e.data; }); document.getElementById('button').onclick = function () { data += 100; sendData(data); } } </script> </body> </html>
##
/** * 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 ( <View style={styles.container}> <View style={{width: 375, height: 220}}> <WebView ref={'webview'} source={require('./index.html')} style={{width: 375, height: 220}} onMessage={(e) => { this.handleMessage(e) }} /> </View> <Text>来自webview的数据 : {this.state.webViewData}</Text> <Text onPress={() => { this.sendMessage() }}>发送数据到WebView</Text> </View> ) } } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 22, backgroundColor: '#F5FCFF', }, });
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!