如何使用RCTRootView的React Native启动选项参数

Avn*_*arr 6 facebook ios reactjs react-native

我不是JS或React程序员,但我需要将它用于我的项目.

我想从我的本机代码将值传递给我的React视图,以便它将由React引擎呈现.

我尝试了很多变化

我的本机代码如下:

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"ReactTesting"
                                                   launchOptions:@{@"initialProps" : @"<Text>Hello from native code</Text>"}];

rootView.initialProperties = @{@"initialProps" : @"<Text>Hello from native code</Text>"}; // ???
Run Code Online (Sandbox Code Playgroud)

在我的反应代码中,我想渲染JSX:

var ReactForTesting = React.createClass( {

render: function() {
  return (The Launch Options that were sent to this view); //How do I render it?
  }
});
Run Code Online (Sandbox Code Playgroud)

我已经尝试在渲染函数中返回各种东西 - 没有渲染:

render : function () {
  return this.props; // doesn't work
}

render : function () {
  return this.props.initialProps; // doesn't work
}

render : function () {
  return this.props.initialProps; // doesn't work
}

also fiddling with:
React.createElement()
React.createClass()..
Run Code Online (Sandbox Code Playgroud)

Col*_*say 4

在 AppDelegate.m 中:

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

NSDictionary *initialProps = [NSDictionary dictionaryWithObject: @"Hello from native code" forKey: @"myProp"];

rootView.initialProperties = dict;
Run Code Online (Sandbox Code Playgroud)

然后您可以this.props.myProp在您传递给的任何组件中访问AppRegistry.registerComponent

不知道为什么你要尝试在字符串中传递 <Text> 节点,但这似乎是一个坏主意。只需传递文本内容,然后在 JS 端使用 <Text> 组件即可。