简体   繁体   English

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

[英]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.

相关问题 将 React Native RCTRootView 解散回 Native ViewController - Dismiss a React Native RCTRootView back to a Native ViewController 将 RCTRootView 作为子视图添加到 React-Native 中的 rootViewController - Add RCTRootView as a subview to rootViewController in React-Native 如何在视图控制器中使用RCTRootView? - How to use RCTRootView in a view controller? 在UITableViewCell中加载React Native视图(RCTRootView) - Loading a React Native View (RCTRootView) in a UITableViewCell 在React Native中使用nativeID /与RCTRootView子视图一起使用的正确方法是什么? - What is the correct way of using nativeID / working with RCTRootView subviews in React Native? React Native 0.66 iOS 在 AppDelegate RCTRootView init 上崩溃 - React Native 0.66 iOS crashes on AppDelegate RCTRootView init 为什么 React Native 视图不使用 RCTRootView 加载? - Why React Native view doesn't load using RCTRootView? React-Native Hybrid App:嵌入RCTRootView的Pop UIViewController - React-Native Hybrid App: Pop UIViewController that embeds RCTRootView -[RCTRootView cancelTouches]` 已弃用,将很快在反应原生 map 中删除 - -[RCTRootView cancelTouches]` is deprecated and will be deleted soon in react native map React-Native与现有应用程序“RCTRootView.h”文件集成未找到 - React-Native Integrating with Existing Apps 'RCTRootView.h' file not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM