前端之家收集整理的这篇文章主要介绍了
React-Native 学习--参数传递,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
原生端 –> RN端
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
...
mReactInstanceManager = ReactInstanceManager.builder()
...
.build();
Bundle bundle = new Bundle();
bundle.putString("name","原生传递参数。。。");
mReactRootView.startReactApplication(mReactInstanceManager,"setting",bundle);
---------------
render(){
let name = this.props.name;
return (
<View style={styles.container}>
<Text>
{name}
</Text>
</View>
)
}
RN端 –> 原生端
import React from 'react';
import { NativeModules } from 'react-native';
module.exports = NativeModules.TToast;
var LToast = NativeModules.TToast;
...
render() {
return (
<View style={styles.container}>
<Text style={styles.hello}
onPress={this._showNativeToast.bind(this)}>
Hello,RN
</Text>
</View>
)
}
...
_showNativeToast(){
LToast.show('传递给原生。。',0);
}
-----------------
public class ToastAndroidModule extends ReactContextBaseJavaModule {
public ToastAndroidModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "TToast";
}
@ReactMethod
public void show(String message,int duration) {
Toast.makeText(getReactApplicationContext(),message,duration).show();
}
}
public class ToastReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ToastAndroidModule(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
public class MyApp extends Application implements ReactApplication {
@Override
public void onCreate() {
super.onCreate();
FileUtils.init(this);
}
private ReactNativeHost mHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.asList(new MainReactPackage(),new ToastReactPackage());
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mHost;
}
}
结束
继承ReactActivity的,貌似,没传参成功。。。。。。