ila*_*sas 13 javascript android properties objective-c react-native
我在React-Native下工作,我正在寻找通过Java将初始道具传递给JS.这可以在Objective-C中使用initialProperties轻松完成,如下所示:
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"myapp"
initialProperties:initialProperties
launchOptions:launchOptions];
Run Code Online (Sandbox Code Playgroud)
其中initialProperties是一个NSDictionary将在JSON中转换并在JS中可用的this.props.所以我想在Android中做同样的事情.有帮助吗?谢谢
Dav*_*ski 16
在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);
Run Code Online (Sandbox Code Playgroud)
Mic*_*ele 11
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;
}
};
}
Run Code Online (Sandbox Code Playgroud)
小智 5
更新 react-native > 0.59.0 你需要在你的 MainActivity.java
例子
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";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4971 次 |
| 最近记录: |