react与native异步交互的几种方式

1:通过函数回调,传递一个string或者一个object 不能传递json如果想传递json格式 可以将json转成string 到react端再解析会json

2:通过promise 使用async,await 传递一个map

3:通过事件native端可以发送参数到js 传递一个map

1:代码:

/**
 * index.android.js
 */
 'use strict';
 import React, {
   AppRegistry,
   Component,
   StyleSheet,
   Text,
     Image,
   TouchableHighlight,
   View,
     NativeModules
 } from 'react-native';
 var path = require('./Lighthouse.jpg');
 var  QRFromAlbum = require('./mymodule/QRFromAlbum');
 class TestQRAndroidExample extends React.Component{
 constructor(props) {
     super(props);
     this.state = {
       torchMode: 'off',
       cameraType: 'back',
         flag:false,
         Imagepath:null,
     };
   }
     onClick(){
     <strong><span style="font-size:18px;"> QRFromAlbum.chooseImage((res)=>{
          var json = JSON.parse(res);
          var path = json.imagepath;//得到图片的路径
          this.setState({
              flag:true,
              Imagepath:path,
          });</span></strong>
          });
  }

 render() {
     if(this.state.flag){
         console.log("path:"+this.state.Imagepath);
         return (<View style={styles.main}>
                 <Image style={styles.containerImageView}
                        source={
  {uri:this.state.Imagepath}}>
                 </Image>
         </View>
         );
     }else{
     
         return (<View style={styles.main}>
             <TouchableHighlight style={styles.button} underlayColor='#99d9fe' onPress={()=>{this.onClick();}}>
                 <Text style={styles.buttontext}>打开相册 </Text>
             </TouchableHighlight>
         </View>
       );
     }
 }
 };
 var styles = StyleSheet.create({
     containerTextView: {
        flex: 1,
   },
   containerImageView:{
         flex: 1,
         position:'relative',
         width:300,
         height:300,
       flexDirection:'row',
       justifyContent:'center',
   },
   
     text:{
           position:'relative',
           color:'white',
           flex:1,
           bottom:160,
           left:90,
           height:60,
           fontSize: 15,
           fontWeight: 'bold',
           flexDirection:'row',
          justifyContent:'center',
      },
       button:{
                 position:'relative',
                 flex:1,
                 backgroundColor:'#48BBEC',
                 bottom:170,
                 left:100,
                 height:35,
                 flexDirection:'row',
                 justifyContent:'center',
                 borderRadius:10,
            },
          buttontext:{
            flex:1,
            fontSize:15,
            color:'white',
            alignSelf:'center',
            },
      main:{
          flex:1,
          position:'absolute',
      },
 });

 AppRegistry.registerComponent('TestUI', () => TestQRAndroidExample);
Java代码:

module部分

package com.nativemodule;

import android.app.Activity;
import android.content.Intent;
i
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值