React Native - initialProperties Android React Native - initialProperties Android android android

React Native - initialProperties Android


In Android, you can pass initialProps in with the launchOptions as a Bundle.

As is mentioned here in the source code: https://github.com/facebook/react-native/blob/7377fdcc70b25eb023e7c6d1b37eeae2a700cb88/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L325-L334

So you can do something like this:

Bundle initialProps = new Bundle();initialProps.putString("myKey", "myValue");mReactRootView.startReactApplication(mReactInstanceManager, "MyAwesomeApp", initialProps);


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. */@Overrideprotected String getMainComponentName() {    return "myAppName";}@Overrideprotected 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;        }    };}


update for react-native > 0.59.0 you need override ReactActivityDelegate in your MainActivity.java

Example

import android.os.Bundle;import androidx.annotation.Nullable;import com.facebook.react.ReactActivity;import com.facebook.react.ReactActivityDelegate;public class RNTesterActivity extends ReactActivity {  public static class RNTesterActivityDelegate extends ReactActivityDelegate {    public RNTesterActivityDelegate(ReactActivity activity, String mainComponentName) {      super(activity, mainComponentName);    }    @Override    protected Bundle getLaunchOptions() {       // YOUR PROPS       Bundle props = new Bundle();       props.putString("key1", "string");       props.putInt("key2", 5);       return props;    }  }  @Override  protected ReactActivityDelegate createReactActivityDelegate() {    return new RNTesterActivityDelegate(this, getMainComponentName());  }  @Override  protected String getMainComponentName() {    return "RNTesterApp";  }}