简体   繁体   中英

How to pass props from android native code to react native

I'm trying to pass some attributes from android native code (Java) to react-native. For now I do it in the initialization, but later it will be done from a function.

I read through the docs and tried to copy the code the official docs submitted, however this did not work.

Inside my MainActivity class:

@Override
protected Bundle getLaunchOptions() {
    Bundle initialProperties = new Bundle();
    initialProperties.putString("testString", "This should be displayed now");
    return initialProperties;
}

In my app.js render method:

<Text>{this.props.testString}</Text>

The rest of the app works fine, however I get a blank space where the text should be. Debugger says its undefined/null

You missed the return part :

@Override
protected Bundle getLaunchOptions() {
    Bundle initialProperties = new Bundle();
    initialProperties.putString("testString", "This should be displayed now");
    return initialProperties;
}

What @Logan Wlv answered is partially correct but you need to configure it so that it uses promise

@Override protected Bundle getLaunchOptions(promise Promise) //change here
{
Bundle initialProperties = new Bundle();
initialProperties.putString("testString", "This should be displayed 
now");
return promise.resolve(initialProperties;//change here 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM