在 React Native 中调用导出的方法
Call exported method in react native
我想在 Objective C 中创建一个视图并在 react native 中使用,但不知道该怎么做
这是我的代码:
目标 C:
#import "DGTAuthenticateButtonView.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import <DigitsKit/DigitsKit.h>
@implementation DGTAuthenticateButtonView
RCT_EXPORT_MODULE()
- (UIView *) view {
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"REGISTER" forState:UIControlStateNormal];
return button;
}
RCT_EXPORT_METHOD(authenticate) {
Digits *digits = [Digits sharedInstance];
DGTAuthenticationConfiguration *configuration = [[DGTAuthenticationConfiguration alloc] initWithAccountFields:DGTAccountFieldsDefaultOptionMask];
configuration.phoneNumber = @"+345555555555";
[digits authenticateWithViewController:nil configuration:configuration completion:^(DGTSession *newSession, NSError *error){
}];
}
@end
我想在 TouchableOpacity 中调用 authenticate
但它不起作用:(
import React, {Component} from 'react';
import {
AppRegistry,TouchableOpacity
} from 'react-native';
var requireNativeComponent = require('requireNativeComponent');
var DGTAuthenticateButtonView = requireNativeComponent('DGTAuthenticateButtonView', DGTAuthenticateButtonView);
class TestProj extends Component {
render() {
return (
<TouchableOpacity style={{flex: 1, backgroundColor: 'green'}}
onPress={() => DGTAuthenticateButtonView.authenticate()}
/>
)
}
}
AppRegistry.registerComponent('TestProj', () => TestProj);
有人知道怎么做吗?提前致谢。
看来你在这里混合了 2 个不同的概念。
您可以创建一个 Native UI Component - a native view that you can use as a component in your RN render
functions; or you can create a Native Module - 一个允许您添加本机功能并从您的 JS 代码调用它的模块,该模块没有视图。
据我所知(您没有包含 RCTViewManager
子类的代码),您正试图在本机端创建一个 Native UI Components
(returns 视图),但不要在你的 JS 中使用它(不用作你的 render
中的组件)。您还应该知道,您不能像在这里尝试的那样直接在本机视图上直接调用方法。
我建议您使用以下解决方案之一:
- 如果您确实需要自定义原生 UI - 请按照创建
Native UI Component
的说明进行操作,然后在中使用您的组件
你的渲染功能。然后,您可以使用道具传达按钮按下
映射到回调(请参阅 here 关于从本机到 JS 的事件)。
- 如果您不需要自定义 UI(您想继续使用示例中的
TouchableOpacity
),您可以按照说明创建 Native Module
。然后,您将能够静态调用 authenticate
方法,就像您在此处尝试执行的那样,以仅执行本机逻辑。您甚至可以修改 authenticate
方法以接收其他参数 - reject/resolve 承诺或回调以在完成时通知 JS。
我想在 Objective C 中创建一个视图并在 react native 中使用,但不知道该怎么做 这是我的代码: 目标 C:
#import "DGTAuthenticateButtonView.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import <DigitsKit/DigitsKit.h>
@implementation DGTAuthenticateButtonView
RCT_EXPORT_MODULE()
- (UIView *) view {
UIButton *button = [[UIButton alloc] init];
[button setTitle:@"REGISTER" forState:UIControlStateNormal];
return button;
}
RCT_EXPORT_METHOD(authenticate) {
Digits *digits = [Digits sharedInstance];
DGTAuthenticationConfiguration *configuration = [[DGTAuthenticationConfiguration alloc] initWithAccountFields:DGTAccountFieldsDefaultOptionMask];
configuration.phoneNumber = @"+345555555555";
[digits authenticateWithViewController:nil configuration:configuration completion:^(DGTSession *newSession, NSError *error){
}];
}
@end
我想在 TouchableOpacity 中调用 authenticate
但它不起作用:(
import React, {Component} from 'react';
import {
AppRegistry,TouchableOpacity
} from 'react-native';
var requireNativeComponent = require('requireNativeComponent');
var DGTAuthenticateButtonView = requireNativeComponent('DGTAuthenticateButtonView', DGTAuthenticateButtonView);
class TestProj extends Component {
render() {
return (
<TouchableOpacity style={{flex: 1, backgroundColor: 'green'}}
onPress={() => DGTAuthenticateButtonView.authenticate()}
/>
)
}
}
AppRegistry.registerComponent('TestProj', () => TestProj);
有人知道怎么做吗?提前致谢。
看来你在这里混合了 2 个不同的概念。
您可以创建一个 Native UI Component - a native view that you can use as a component in your RN render
functions; or you can create a Native Module - 一个允许您添加本机功能并从您的 JS 代码调用它的模块,该模块没有视图。
据我所知(您没有包含 RCTViewManager
子类的代码),您正试图在本机端创建一个 Native UI Components
(returns 视图),但不要在你的 JS 中使用它(不用作你的 render
中的组件)。您还应该知道,您不能像在这里尝试的那样直接在本机视图上直接调用方法。
我建议您使用以下解决方案之一:
- 如果您确实需要自定义原生 UI - 请按照创建
Native UI Component
的说明进行操作,然后在中使用您的组件 你的渲染功能。然后,您可以使用道具传达按钮按下 映射到回调(请参阅 here 关于从本机到 JS 的事件)。 - 如果您不需要自定义 UI(您想继续使用示例中的
TouchableOpacity
),您可以按照说明创建Native Module
。然后,您将能够静态调用authenticate
方法,就像您在此处尝试执行的那样,以仅执行本机逻辑。您甚至可以修改authenticate
方法以接收其他参数 - reject/resolve 承诺或回调以在完成时通知 JS。