【问题标题】:How to use React Native launch options parameter for RCTRootView如何为 RCTRootView 使用 React Native 启动选项参数
【发布时间】:2015-09-22 13:54:24
【问题描述】:

我不是 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>"}; // ???

在我的反应代码中,我想渲染那个 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()..

【问题讨论】:

    标签: ios facebook reactjs react-native


    【解决方案1】:

    这是我解决此问题的方法:https://github.com/facebook/react-native/issues/2100

    您可以在 settings.plist 中定义这些初始道具

    干杯

    【讨论】:

    • 嗨,我想知道为什么我们必须在我们的视图控制器中设置根视图而不是应用程序委托?设置根视图有什么用?你能解释清楚吗Lz...
    【解决方案2】:

    在 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

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

    【讨论】:

    • 我想从服务器获取节点(作为字符串)并让 React 处理渲染它们。否则我需要在我的反应代码中创建一个解析器来理解文本(节点的描述)并发出适当的反应代码来生成标签
    • 那不行,你会从 Obj-C 端收到一个字符串,它们不是组件。您仍然需要解析字符串。
    • 好的。谢谢。你知道在设备上而不是在节点服务器上编译 JSX 的方法吗?
    • 我会就此提出一个新问题,我们可以在那里讨论。
    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2021-12-01
    相关资源
    最近更新 更多