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

版权声明:转载请注明出处 https://blog.csdn.net/menwaiqingshan/article/details/81974818

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
    }
}

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

猜你喜欢

转载自blog.csdn.net/menwaiqingshan/article/details/81974818