Hi,
I'm looking for passing initial props to JS via Java. This can be done easily in Objective-C with initialProperties like this :
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"myapp"
initialProperties:initialProperties
launchOptions:launchOptions];
Where initialProperties is an NSDictionary which will be converted in JSON and available in JS via this.props.
So I'm looking firstly if this is possible in Android and if yes, does any examples or doc exists ? Thanks
Hey ilansas, thanks for reporting this issue!
React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.
react-native or for more real time interactions, ask on Discord in the #react-native channel.reactRootView.startReactApplication(mReactInstanceManager, "UIExplorerApp", initialProperties)
Great thanks a lot !
@baoti or @ilansas - where exactly should reactRootView.startReactApplication go in my app?
Found it. Override the getLaunchOptions method of MainActivity.java:
@Override
protected Bundle getLaunchOptions() {
Bundle opts = new Bundle();
opts.putBoolean("someValue", true);
return opts;
}
hi, howa can i pass Map in the getLaunchOptions funtion at the Android
seems like this is deprecated in react-native > 0.34
https://github.com/facebook/react-native/pull/9320
But I'm on 0.30 and cannot get it to work
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 "####masked####";
}
/**
*
* move to ReactActivityDelegate if react-native > 0.34
* https://github.com/facebook/react-native/pull/9320
*/
@Override
protected @Nullable Bundle getLaunchOptions() {
Bundle opts = new Bundle();
opts.putString("HOSTNAME", "localhost");
opts.putInt("PORT", 8000);
opts.putBoolean("SECURE", false);
return opts;
}
}
getlauchOptions has been moved inside ReactActivityDelegate, now I use this code:
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;
}
};
}
}
@micheletedeschi got error said below, any help?
/Users/tongchepluto/Desktop/JM/yjsPro/android/app/src/main/java/com/yjspro/MainActivity.java:21: 错误: 找不到符号
protected Bundle getLaunchOptions() {
^
符号: 类 Bundle
/Users/tongchepluto/Desktop/JM/yjsPro/android/app/src/main/java/com/yjspro/MainActivity.java:22: 错误: 找不到符号
Bundle initialProps = new Bundle();
^
符号: 类 Bundle
/Users/tongchepluto/Desktop/JM/yjsPro/android/app/src/main/java/com/yjspro/MainActivity.java:22: 错误: 找不到符号
Bundle initialProps = new Bundle();
^
符号: 类 Bundle
3 个错误
ops, missing import android.os.Bundle; new at android ,thanks a lot 🍻
by the way still need import com.facebook.react.ReactActivityDelegate;
How can i pass a callback ??
Is there a proper/official method to update these initialProps ? Currently I implemented a native method to send an object when it has changed, but feel free to give me your tips !
@micheletedeschi How can I pass object as initial props?
Most helpful comment
hi, howa can i pass Map in the getLaunchOptions funtion at the Android