【问题标题】:How to read initial properties in React Native如何在 React Native 中读取初始属性
【发布时间】:2018-07-17 13:58:51
【问题描述】:

我正在将初始属性传递给我的 React Native 应用程序。

NSDictionary *dictionary = @{@"testID": @"123"};

这是在 React Native 端收到的,并显示

{"rootTag":1,"initialProps":{"testID":"123"}}.

但是,我正在读取 React Native 端的数据 -

componentWillMount() {
  var token = this.props.testID;
  console.log("testID is " + token);
}

它给出错误 JSON 值 NSNull 无法转换为 NSString。

testID 未定义

【问题讨论】:

  • 代码 sn-p @{"testID": @"123"} 正确吗?在 "testID" 文字之前应该有一个 @ 符号。

标签: objective-c react-native react-native-ios


【解决方案1】:

您的字典中缺少@。考虑以下示例来传递初始道具:

NSArray *imageList = @[@"http://foo.com/bar1.png",
                       @"http://foo.com/bar2.png"];

NSDictionary *props = @{@"images" : imageList};

RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                             moduleName:@"ImageBrowserApp"
                                             initialProperties:props];

您可以在communication-ios 部分阅读更多内容。

【讨论】:

    猜你喜欢
    • 2016-03-07
    • 2017-06-14
    • 2019-03-12
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2016-11-16
    相关资源
    最近更新 更多