当前位置:   jquery > 正文

React Native - initialProperties Android

javascript,android,properties,objective-c,react-native,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

我在React-Native下工作,我正在寻找通过Java将初始道具传递给JS.这可以在Objective-C中使用initialProperties轻松完成,如下所示:

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"myapp"
                                               initialProperties:initialProperties
                                                   launchOptions:launchOptions];

其中initialProperties是一个NSDictionary将在JSON中转换并在JS中可用的this.props.所以我想在Android中做同样的事情.有帮助吗?谢谢



1> Dave Sibiski..:

在Android中,你可以通过initialProps与在launchOptions成捆.

正如源代码中所述:https://github.com/facebook/react-native/blob/7377fdcc70b25eb023e7c6d1b37eeae2a700cb88/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L325-L334

所以你可以这样做:

Bundle initialProps = new Bundle();
initialProps.putString("myKey", "myValue");

mReactRootView.startReactApplication(mReactInstanceManager, "MyAwesomeApp", initialProps);


是否可以传递结构化数据,类似于将值的NSDictionary传递给iOS中的React Native的方式?还是仅可能标量值?

2> 小智..:

getlauchOptions已在ReactActivityDelegate中移动,现在我使用此代码:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/jquery/detail/13279
推荐阅读
相关标签
  

闽ICP备14008679号