[英]How to use React Native launch options parameter for RCTRootView
I am not a JS or React programmer but I need to use it for my project. 我不是JS或React程序员,但我需要将它用于我的项目。
I want to pass in values to my React views from my native code so that it will be rendered by the React engine. 我想从我的本机代码将值传递给我的React视图,以便它将由React引擎呈现。
I've tried many variations 我尝试了很多变化
My native code looks like: 我的本机代码如下:
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>"}; // ???
And in my react code i want to render that JSX: 在我的反应代码中,我想渲染JSX:
var ReactForTesting = React.createClass( {
render: function() {
return (The Launch Options that were sent to this view); //How do I render it?
}
});
I've tried returning various things in the render function - none would render: 我已经尝试在渲染函数中返回各种东西 - 没有渲染:
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()..
In AppDelegate.m: 在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;
Then you can access this.props.myProp
in whichever component you passed to AppRegistry.registerComponent
. 然后,你可以访问this.props.myProp
你传递给哪个组件AppRegistry.registerComponent
。
Not sure why you're trying to pass a <Text> node through in your string though, that seems like a bad idea. 不知道为什么你试图在你的字符串中传递一个<Text>节点,这似乎是一个坏主意。 Just pass the text content then use the <Text> component on the JS side. 只需传递文本内容,然后使用JS端的<Text>组件。
Here is the way I have fixed this problem: https://github.com/facebook/react-native/issues/2100 这是我解决这个问题的方法: https : //github.com/facebook/react-native/issues/2100
You can just define those initial props in a settings.plist
您可以在settings.plist
定义这些初始道具
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.