库未与 0.41 版本的 react-native 链接

Library not linking with 0.41 versions of react-native

即使在多次尝试 link 手动 link 之后,我仍然在将库 link 连接到反应本机项目时遇到问题。我学习了很多 "praised" 教程,但都没有成功。

我不是 Objective-C/iOS-native 方面的专家,因此为了隔离问题,我决定创建一个 HelloWorld 库和一个 HelloWorld react-native 项目。

使用:

我做了以下事情:

  1. react-native-create-library Lib.
  2. react-native init RNLibTest.
  3. "Successfully linking"(手动和 react-native link ... 都试过了)

代码如下:

RNLib

// ./ios/RNLib.h
#import <React/RCTBridgeModule.h>

@interface RNLib : NSObject <RCTBridgeModule>

@end



// ./ios/RNLib.m
#import "RNLib.h"

@implementation RNLib

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(helloWorld:(NSString *)world)
{
  return [NSString stringWithFormat:@"hello %@", world];
}

@end



// ./index.js
import { NativeModules } from 'react-native';

const { RNLib } = NativeModules;
console.log(RNLib); // undefined
console.log(NativeModules); // Object: RNLib NOT included

export default RNLib;

HelloWorld/RNLibTest

// index.ios.js

import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';
import RNLib from 'react-native-lib'; // I installed RNLib with npm and then "linked".

export default class RNLibTest extends Component {
  componentDidMount() {
    console.log(RNLib); // undefined
  }

  render() {
    return (
      <View>
        <Text>Hello World</Text>
        <Text>{
          RNLib.helloWorld("world") 
          /* throws error "can't read property helloWorld of undefined" */
        }</Text>
      </View>
    );
  }
}

AppRegistry.registerComponent('RNLibTest', () => RNLibTest);

我的问题:

  1. 如果代码没有错,会不会是react-create-native-library创建的xcode项目不知何故无法与较新版本的react-native一起使用?
  2. 还有其他人遇到这些问题吗?如果是这样,您是如何解决的?
  3. 如果你没有遇到过这个问题,你使用的是最新版本的react-native吗?
  4. 您使用的是什么版本的 xcode、react-native-cli 等...?
  5. 有人需要降级才能解决这个问题吗?

更新Feb/22/2017

有一个新版本的 react-native-create-library (v1.1.0) 支持 v0.40+。我再次尝试并更新了上面的代码,但我仍然看到同样的问题。

Here is a link to the GitHub issue. I also uploaded react-native-lib library and LibTest app 到 GitHub.

不确定 react-native-create-library 是否支持 0.41 RN 但是从代码中我可以说你需要导入 RCT*h 文件,比如 <React/RCTBridgeModule.h>

因为这是单一的很好,你可以在 Xcode 中创建一个 RNLib.h 文件并检查你是否会遇到编译时错误

谢谢

我不知道是什么导致了这个问题,但是随着 react-native 0.42 的出现,我决定再试一次。

相同的代码、相同的库、相同的一切,但 react-native 0.42,这些是我的结果:

  1. 使用 react-native link => 成功了。
  2. 使用手动安装 => 成功。

我不确定 0.41 中发生了什么,但 0.42 中没有发生!