ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

H5调用React Native

2021-11-14 13:32:07  阅读:282  来源: 互联网

标签:.. H5 React window let userInfo props secServiceCode Native


H5代码如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./build/calendar.css">
    <style>
        .btn{
            background: #f00;color:#fff
        }
    </style>
</head>
<body style="background:#ddd;">
<div style="text-align: center">
    <button id="button" onclick="send()">发送数据到react native</button>
    <p style="text-align: center">收到react native发送的数据: <span id="data"></span></p>
</div>
</body>
<script>
    var data = 0;
    function send () {
        data += 100;
        //window.ReactNativeWebView.postMessage("Hello React");

        window.postMessage("Hello React");
    }
    window.onload = function () {
        alert(111);
    }
</script>
</html>

RN代码如下:

import React, { Component } from 'react';
import {
  WebView,
  NativeModules,
  AppRegistry,
  Dimensions,
  View,
  Image,
  Text,
} from 'react-native';
import NavigationBar from '../../component/NavBarComponent';
import BehaviorUtils from '../../util/BehaviorUtil';
import publicStyle from '../../util/publicStyle';

var screenWidth = Dimensions.get('window').width;
var screenHeight = Dimensions.get('window').height;
var nativeModule = NativeModules.OpenHomeModule;
var publicModule = NativeModules.PublicModule;

/**
* desc:H5页面
* author:jiaqch@neusoft.com
* date:2019-09-03
*/
export default class Pageh5 extends Component<Props>{

  constructor(props){
    super(props);
    this.state = {
      hasNet:true,
    }
  }

  //返回首页
  openHomePage(){
      let backFlag = this.props.backFlag;
      if(backFlag && backFlag=='1'){
        this.back();
      }else{
        nativeModule.openHomePage();
      }
  }

  //返回
  back(){
      const{navigator} = this.props;
      if(navigator){
        navigator.pop();
      }
  }

  componentDidMount() {
      let secServiceCode = this.props.userInfo[0].secServiceCode;
      //二级服务代码不空的情况下跟踪
      if(secServiceCode && secServiceCode!=""){
        let userId = this.props.userInfo[0].userId;
        let cityCode = this.props.userInfo[0].cityCode;
        let phoneNumber = this.props.userInfo[0].phoneNumber;
        let deviceId = this.props.userInfo[0].deviceId;
        let ipAddress = this.props.userInfo[0].ipAddress;
        BehaviorUtils.sendBmgjData(userId,secServiceCode,"1","点击",cityCode,phoneNumber,deviceId,ipAddress);
      }
  }

   onMessage(e){
      console.log('serviceShare:...');
      publicModule.serviceShare(e.nativeEvent.data);
   }




  render() {
    return (
    <View style={{flex: 1,backgroundColor:'#fff'}}>
      <NavigationBar title={this.props.userInfo[0].secServiceName}
                leftAction={()=>{this.openHomePage()}}>
              </NavigationBar>
      <View style={{flex: 1}}>
          {
            this.state.hasNet?
            <WebView
              ref='webview'
              useWebKit={true}
              startInLoadingState={true}
              domStorageEnabled={true}//开启dom存贮
              javaScriptEnabled={true}//开启js
              scalesPageToFit={true}
              geolocationEnabled={true}
              mixedContentMode={'always'}
              source={{uri:this.props.userInfo[0].serviceUrl}}
              one rror={()=>{this.setState({hasNet:false})}}
              onMessage={this.onMessage.bind(this)}
            />:
            <View style={{height:screenHeight-100,justifyContent:'center',alignItems:'center'}}>
              <Image source={require('../../image/gjcx/nowifi.png')} style={{width:181,height:164}}/>
              <Text style={[publicStyle.font14_GrayR,{marginTop:10}]}>网络请求失败</Text>
              <Text style={[publicStyle.font14_GrayR,{marginTop:5}]}>请检查你的网络</Text>
            </View>
          }
      </View>
    </View>
    );
  }
}

标签:..,H5,React,window,let,userInfo,props,secServiceCode,Native
来源: https://blog.csdn.net/u010675594/article/details/121316547

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有