react-native icon indicating copy to clipboard operation
react-native copied to clipboard

initialProperties in Android

Open ilansas opened this issue 10 years ago • 13 comments

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

ilansas avatar Dec 02 '15 16:12 ilansas

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!

facebook-github-bot avatar Dec 02 '15 17:12 facebook-github-bot

reactRootView.startReactApplication(mReactInstanceManager, "UIExplorerApp", initialProperties)

baoti avatar Dec 03 '15 14:12 baoti

Great thanks a lot !

ilansas avatar Dec 03 '15 15:12 ilansas

@baoti or @ilansas - where exactly should reactRootView.startReactApplication go in my app?

lynndylanhurley avatar Feb 18 '16 21:02 lynndylanhurley

Found it. Override the getLaunchOptions method of MainActivity.java:

  @Override
  protected Bundle getLaunchOptions() {
    Bundle opts = new Bundle();
    opts.putBoolean("someValue", true);
    return opts;
  }

lynndylanhurley avatar Feb 18 '16 22:02 lynndylanhurley

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

skeyboy avatar Sep 07 '16 05:09 skeyboy

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

Thibaut-Fatus avatar Sep 13 '16 13:09 Thibaut-Fatus

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 avatar Oct 01 '16 11:10 micheletedeschi

@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 个错误

pppluto avatar Feb 28 '17 08:02 pppluto

ops, missing import android.os.Bundle; new at android ,thanks a lot 🍻 by the way still need import com.facebook.react.ReactActivityDelegate;

pppluto avatar Feb 28 '17 09:02 pppluto

How can i pass a callback ??

luoyushouchai avatar Mar 15 '17 05:03 luoyushouchai

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 !

StevenTB avatar May 29 '17 14:05 StevenTB

@micheletedeschi How can I pass object as initial props?

strmchsr avatar Apr 12 '18 12:04 strmchsr