我在React-Native下工作,我正在寻找通过Java将初始道具传递给JS.这可以在Objective-C中使用initialProperties轻松完成,如下所示:
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"myapp" initialProperties:initialProperties launchOptions:launchOptions];
其中initialProperties是一个NSDictionary
将在JSON中转换并在JS中可用的this.props
.所以我想在Android中做同样的事情.有帮助吗?谢谢
在Android中,你可以通过initialProps
与在launchOptions
成捆.
正如源代码中所述:https://github.com/facebook/react-native/blob/7377fdcc70b25eb023e7c6d1b37eeae2a700cb88/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L325-L334
所以你可以这样做:
Bundle initialProps = new Bundle(); initialProps.putString("myKey", "myValue"); mReactRootView.startReactApplication(mReactInstanceManager, "MyAwesomeApp", initialProps);
getlauchOptions已在ReactActivityDelegate中移动,现在我使用此代码:
public class MainActivity extends ReactActivity { /** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */ @Override protected String getMainComponentName() { return "myAppName"; } @Override protected ReactActivityDelegate createReactActivityDelegate() { return new ReactActivityDelegate(this, getMainComponentName()) { @Nullable @Override protected Bundle getLaunchOptions() { Bundle initialProps = new Bundle(); initialProps.putString("SOME_VARIABLE_1", BuildConfig.SOME_VARIABLE_1); initialProps.putString("SOME_VARIABLE_2", "some variable 2 value"); return initialProps; } }; }