我不是 JS 或 React 程序员,但我需要在我的项目中使用它。
我想将值从我的 native 代码传递到我的 React View ,以便它由 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>"}; // ???
在我的 React 代码中,我想渲染那个 JSX:
var ReactForTesting = React.createClass( {
render: function() {
return (The Launch Options that were sent to this view); //How do I render it?
}
});
我试过在渲染函数中返回各种东西——没有一个会渲染:
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()..
最佳答案
在 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;
然后您可以在传递给 AppRegistry.registerComponent
的任何组件中访问 this.props.myProp
。
但不确定为什么要尝试在字符串中传递
关于ios - 如何使用 RCTRootView 的 React Native 启动选项参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228560/