【发布时间】:2017-02-16 18:30:08
【问题描述】:
即使在尝试手动链接多个不同时间后,我在将库链接到 react-native 项目时也遇到问题。我已经完成了大量“称赞”的教程,但都没有成功。
我不是 Objective-C/iOS-native 方面的专家,所以为了解决这个问题,我决定创建一个 HelloWorld 库和一个 HelloWorld react-native 项目。
使用:
- react-native v0.41.2
- react-native-cli v2.0.1
- react-native-create-library v1.0.4
- xcode v8.2.1
我做了以下事情:
-
react-native-create-library Lib。 -
react-native init RNLibTest。 - “链接成功”(手动和
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);
我的问题:
- 如果代码没有错,难道是 react-create-native-library 创建的 xcode 项目无法与较新版本的 react-native 一起使用?
- 还有其他人有这些问题吗?如果是这样,您采取了哪些措施来解决它?
- 如果你还没有遇到这个问题,你是用最新版的 react-native 吗?
- 您使用的是什么版本的 xcode、react-native-cli 等...?
- 是否有人必须降级才能解决此问题?
2017 年 2 月 22 日更新
有一个新版本的 react-native-create-library (v1.1.0) 支持 v0.40+。我再次尝试并更新了上面的代码,但我仍然看到同样的问题。
Here is a link to the GitHub issue。我还把react-native-lib library和LibTest app上传到了GitHub。
【问题讨论】:
标签: ios objective-c xcode react-native react-native-ios