Android 原生项目集成React Native——传参

Android 原生项目集成React Native——传参

在加载js的同时传参,需要区分Activity中两种集成方式来讲。

 

通过Bundle传参

 

把js文件当成是布局加载的传参

只需要在startReactApplicationc传入initialProperties的参数就可以了

Bundle bundle=new Bundle();
bundle.putString("token","我传过来1");
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", bundle);

 

继承ReactActivity的传参

查看ReactActivity的源码,得知startReactApplication的源码在ReactActivityDelegate的loadApp函数中,所以重写createReactActivityDelegate。

@Override
    public ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {
            @Override
            protected Bundle getLaunchOptions() {
                Bundle bundle=new Bundle();
                bundle.putString("param","我是从ReactTwoActivity传过来的");
                return bundle;
            }
        };
    }
}

 

js页面接收数据

组件构造函数的props就能收到

constructor(props) {
    super(props);
    this.state={
        param:this.props.param,//这个this.props.param的param就是bundle的key
    }
}

跳转页面同时传参大概就是这样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值