React-native: initialProperties in Android

Created on 2 Dec 2015  ·  13Comments  ·  Source: facebook/react-native

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

Locked

Most helpful comment

hi, howa can i pass Map in the getLaunchOptions funtion at the Android

All 13 comments

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.

  • If this is a feature request or a bug that you would like to be fixed by the team, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • If you don't know how to do something or not sure whether some behavior is expected or a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • We welcome clear issues and PRs that are ready for in-depth discussion; thank you for your contributions!
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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oney picture oney  ·  3Comments

axelg12 picture axelg12  ·  3Comments

DreySkee picture DreySkee  ·  3Comments

phongyewtong picture phongyewtong  ·  3Comments

lazywei picture lazywei  ·  3Comments