簡體   English   中英

在React Native中調用導出的方法

[英]Call exported method in react native

我想在Objective C中創建一個視圖並在react native中使用,但是不知道如何執行這是我的代碼:Obj-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個不同的概念。

您可以創建本機UI組件 -可以在RN render函數中用作組件的本機視圖;也可以創建本機UI組件 或者您可以創建一個本機模塊 -一個允許您添加本機功能並從JS代碼中調用它的模塊,該模塊沒有視圖。

據我所知(您未包括RCTViewManager子類的代碼),您正在嘗試在本機端創建本Native UI Components (返回一個視圖),但未在JS中使用它(不用作render的組件)。 您還應該知道,不能像在此處嘗試那樣直接在本機視圖上直接調用方法。

我建議您使用以下解決方案之一:

  1. 如果您確實需要自定義的本機UI,請按照說明創建本Native UI Component ,然后在渲染功能中使用該組件。 然后,您可以使用映射到回調的道具傳達按鈕的按下狀態(有關從本地到JS的事件,請參見此處 )。
  2. 如果您不需要自定義UI(您想像示例一樣繼續使用TouchableOpacity ),則可以按照說明創建本Native Module 然后,您將可以嘗試在此處嘗試僅執行本機邏輯時靜態地調用您的authenticate方法。 您甚至可以修改authenticate方法以接收其他參數-拒絕/解決承諾或回調,以在完成時通知JS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM