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) }} />You can see that there is an onMessage method,
handleMessage(e) { this.setState({webViewData: e.nativeEvent.data}); }e.nativeEvent.data is the data sent from inside the webview.
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:
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.
window.onload = function () { document.addEventListener('message', function (e) { document.getElementById('data').textContent = e.data; }); }This enables communication between Rn and Webview.
<!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>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 ( <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', }, });
Related recommendations:
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!

使用Java13中的新的JavaFXWebView组件来显示网页内容随着Java的不断发展,JavaFX已经成为构建跨平台图形界面的主要工具之一。JavaFX提供了丰富的图形库和组件,让开发者能够轻松地创建各种各样的用户界面。其中,JavaFXWebView组件是一个非常有用的组件,它允许我们在JavaFX应用程序中显示网页内容。在Java13中,J

在react中,antd是基于Ant Design的React UI组件库,主要用于研发企业级中后台产品;dva是一个基于redux和“redux-saga”的数据流方案,内置了“react-router”和fetch,可理解为应用框架。

React不是双向数据流,而是单向数据流。单向数据流是指数据在某个节点被改动后,只会影响一个方向上的其他节点;React中的表现就是数据主要通过props从父节点传递到子节点,若父级的某个props改变了,React会重渲染所有子节点。

Native修饰符的使用native主要用于方法上1、一个native方法就是一个Java调用非Java代码的接口。一个native方法是指该方法的实现由非Java语言实现,比如用C或C++实现。2、在定义一个native方法时,并不提供实现体(比较像定义一个JavaInterface),因为其实现体是由非Java语言在外面实现的。说明Java语言本身不能对操作系统底层进行访问和操作,但是可以通过JNI接口调用其他语言来实现对底层的访问。JNI是Java本机接口(JavaNativeInterf

因为在react中需要利用到webpack,而webpack依赖nodejs;webpack是一个模块打包机,在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,所以react需要使用nodejs。

react是组件化开发;组件化是React的核心思想,可以开发出一个个独立可复用的小组件来构造应用,任何的应用都会被抽象成一颗组件树,组件化开发也就是将一个页面拆分成一个个小的功能模块,每个功能完成自己这部分独立功能。

在react中,forceupdate()用于强制使组件跳过shouldComponentUpdate(),直接调用render(),可以触发组件的正常生命周期方法,语法为“component.forceUpdate(callback)”。

react和reactdom的区别是:ReactDom只做和浏览器或DOM相关的操作,例如“ReactDOM.findDOMNode()”操作;而react负责除浏览器和DOM以外的相关操作,ReactDom是React的一部分。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
