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

标签 ios facebook reactjs react-native

我不是 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

但不确定为什么要尝试在字符串中传递 节点,这似乎是个坏主意。只需传递文本内容,然后在 JS 端使用 组件。

关于ios - 如何使用 RCTRootView 的 React Native 启动选项参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228560/

相关文章:

ios - Facebook iOS 9 SDK 登录在 Safari 上重定向至 mbasic.facebook.com

javascript - React.js import 语句找不到模块

javascript - react : How do I pass a returned function value to a parent component?

javascript - 如何处理使用 facebook javascript sdk 的 Web 应用程序中的 javascript 加载错误

objective-c - iOS Facebook SDK - 最佳实践?

reactjs - 无法读取未定义的属性(读取 'editQuoteModalShown' )react-redux

html - 如何在 Safari 中打开 iPhone 网络应用程序链接?

ios - SwiftUI 中有没有办法更改字符串中特殊字符的颜色?

ios - UITableView - 调整框架大小并滚动到行

ios - 如何实现PSCollectionView?