ReactNative进阶(十):WebView 应用详解_react native webview
创始人
2025-01-15 07:35:16
0
2.1 通过 url 地址加载网页
import React, {Component} from 'react'; import {     AppRegistry,     StyleSheet,     Dimensions,     Text,     View,     WebView } from 'react-native';   //获取设备的宽度和高度 var {     height: deviceHeight,     width: deviceWidth } = Dimensions.get('window');   //默认应用的容器组件 class App extends Component {     //渲染     render() {         return (                            {uri:"https://shq5785.blog.csdn.net/",method: 'GET'}}                 style={{width:deviceWidth, height:deviceHeight}}>                                     );     } }   //样式定义 const styles = StyleSheet.create({     container: {         flex: 1,         paddingTop:20     } });   AppRegistry.registerComponent('HelloWorld', () => App);  
2.2 加载 html 代码
import React, {Component} from 'react'; import {     AppRegistry,     StyleSheet,     Dimensions,     Text,     View,     WebView } from 'react-native';   //获取设备的宽度和高度 var {     height: deviceHeight,     width: deviceWidth } = Dimensions.get('window');   //默认应用的容器组件 class App extends Component {   //渲染   render() {    return (                  {html:"

欢迎访问 https://shq5785.blog.csdn.net/

"}} style={{width:deviceWidth, height:deviceHeight}}>
); } } //样式定义 const styles = StyleSheet.create({ container: { flex: 1, paddingTop:20 } }); AppRegistry.registerComponent('HelloWorld', () => App);
2.3 RN -> HTML5 通信

WebView加载html时,可实现htmlrn之间的通信。rnhtml发送数据可以通过postMessage函数实现。如下:

RN

  (this.webView = view)}     useWebKit={false}     onLoad={() => {       let data = {         name: userInfo.usrName       };       this.webView.postMessage(JSON.stringify(data));     }}     onError={(event) => {       console.log(`==webViewError:${JSON.stringify(event.nativeEvent)}`);     }}     onMessage={(event) => {       this.\_onH5Message(event);     }}     automaticallyAdjustContentInsets={false}     contentInset={{ top: 0, left: 0, bottom: -1, right: 0 }}     onScroll={(event) => this.\_onScroll(event)}     style={styles.webview}     source={this.html ? { html: this.html } : { uri: this.url }}     bounces={false}     showsHorizontalScrollIndicator={false}     showsVerticalScrollIndicator={false}   />  

html

// 在html中注册事件接收rn发过来的数据并显示在html中 document.addEventListener('message', function listener(RnData) {   messagesReceivedFromReactNative += 1;   document.getElementsByTagName('p')[0].innerHTML =     '从React Native接收的消息: ' + messagesReceivedFromReactNative;   document.getElementsByTagName('p')[1].innerHTML = RnData.data;   // 获取接收后的数据后,及时清除监听器   document.removeEventListener('message', listener) });  

html中定义一个按钮,并添加事件向rn发送数据:

//window.postMessage向rn发送数据 document.getElementsByTagName('button')[0].addEventListener('click', function() {   window.postMessage('这是html发送到RN的消息'); });  

html中调用了window.postMessage函数后,WebViewonMessage函数将会被回调,用来处理htmlrn发送的数据,可以通过e.nativeEvent.data获取发送过来的数据。

// 接收HTML发出的数据 _onH5Message = (e) => {   this.setState({       messagesReceivedFromWebView: this.state.messagesReceivedFromWebView + 1,       message: e.nativeEvent.data,   })   Alert.alert(e.nativeEvent.data) }  
2.4 HTML5(Vue) -> RN 通信

HTML5

const message = {  flag: 'previewIamge'  filePath: filePath } window.ReactNativeWebView.postMessage(Json.stringify(message))  

RN
还是通过WebView提供的onMessage 属性完成回调。

  (this.webView = view)}     useWebKit={false}     onLoad={() => {       let data = {         name: userInfo.usrName       };       this.webView.postMessage(JSON.stringify(data));     }}     onError={(event) => {       console.log(`==webViewError:${JSON.stringify(event.nativeEvent)}`);     }}     onMessage={(event) => {       this.\_onH5Message(event);     }}     automaticallyAdjustContentInsets={false}     contentInset={{ top: 0, left: 0, bottom: -1, right: 0 }}     onScroll={(event) => this.\_onScroll(event)}     style={styles.webview}     source={this.html ? { html: this.html } : { uri: this.url }}     bounces={false}     showsHorizontalScrollIndicator={false}     showsVerticalScrollIndicator={false}   />  

回调函数_onH5Message()实现逻辑如下:

// 接收HTML发出的数据 _onH5Message = (e) => {   this.setState({       messagesReceivedFromWebView: this.state.messagesReceivedFromWebView + 1,       message: e.nativeEvent.data,   })   Alert.alert(e.nativeEvent.data) }  

React

  • 介绍一下react

  • React单项数据流

  • react生命周期函数和react组件的生命周期

  • react和Vue的原理,区别,亮点,作用

  • reactJs的组件交流

  • 有了解过react的虚拟DOM吗,虚拟DOM是怎么对比的呢

  • 项目里用到了react,为什么要选择react,react有哪些好处

  • 怎么获取真正的dom

  • 选择react的原因

  • react的生命周期函数

  • setState之后的流程

  • react高阶组件知道吗?

  • React的jsx,函数式编程

  • react的组件是通过什么去判断是否刷新的

  • 如何配置React-Router

  • 路由的动态加载模块

  • Redux中间件是什么东西,接受几个参数

  • redux请求中间件如何处理并发

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...