gpt4 book ai didi

javascript - React Native UI 组件 : RCTBubblingEventBlock/RCTDirectEventBlock do not seem to work

转载 作者:可可西里 更新时间:2023-11-01 03:08:24 28 4
gpt4 key购买 nike

我在 Ignite 中有一个自定义 native View 项目。我正在尝试建立从 Objective-CReact Native 的通信。从 React NativeiOS 的通信使用 HTML 注入(inject),但反之则不行。我试过同时使用 RCTBublingEventBlockRCTDirectEventBlock,但都不起作用。这是我的全部实现。当然,我已经更改了组件的名称,并且只留下了必要的实现,以便您了解到目前为止所做的事情:

Objective-C 代码:

// CustomViewManager.h

#import "RCTViewManager.h"

@interface CustomViewManager : RCTViewManager

@end


// CustomViewManager.m

#import "CustomViewManager.h"
#import "CustomView.h"

#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"

@implementation CustomViewManager

RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(htmlInjection, NSString)
RCT_EXPORT_VIEW_PROPERTY(onEventA, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onEventB, RCTDirectEventBlock)

- (UIView *) view {
return [CustomView new];
}

@end

// CustomView.h

#import "RCTView.h"

@interface CustomView : RCTView

@property (nonatomic, assign) NSString *htmlInjection;
@property (nonatomic, copy) RCTDirectEventBlock onEventA;
@property (nonatomic, copy) RCTDirectEventBlock onEventB;

@end

// CustomView.m

#import "CustomView.h"
#import "RCTUtils.h"

#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import "MyExternalComponent.h"

@interface CustomView () <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView* webView;

@end


- (void) setUpWebView {
if (!_webView) {
[self setWebView: [UIWebView new]];
_webView.delegate = self;
[self addSubview:_webView];
}
}

- (instancetype)init
{
self = [super init];
[self setUpWebView];
return self;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpWebView];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self setUpWebView];
}
return self;
}

- (void) layoutSubviews {
[super layoutSubviews];
CGRect frame = self.frame;
self.webView.frame = frame;
}

#pragma mark - External methods.

- (void) setHtmlInjection:(NSString *)html {
[_webView loadHTMLString:html baseURL:nil];
}

#pragma mark - Non-React component methods.

- (void) fetchData {
[MyExternalComponent getData:^(NSString *dataA, NSError *error){
if(error) {
NSLog(@"Here be errors: %@", error);
_onEventB(@{@"myError": error.localizedDescription});
} else {
_onEventA(@{@"myData": dataA});
}
}]
}

@end

React Native JavaScript 代码:

// MyCustomView.js

import React from 'react';
import { requireNativeComponent } from 'react-native';

class MyCustomView extends React.Component {
constructor(props) {
super(props);
this._onEventA= this._onEventA.bind(this);
this._onEventB= this._onEventB.bind(this);
}
_onEventA(event: Event) {
if (!this.props.onEventA) {
return;
}
this.props.onEventA(event.nativeEvent.myData);
}
_onEventB(event: Event) {
if (!this.props.onEventA) {
return;
}
this.props._onEventB(event.nativeEvent.myError);
}
render() {
return (
<CustomView
{...this.props}
onEventA={this._onEventA}
onEventB={this._onEventB}
/>
);
}
}

MyCustomView.propTypes = {
htmlInjection: React.PropTypes.string,
onEventA: React.PropTypes.func,
onEventB: React.PropTypes.func,
};

var CustomView = requireNativeComponent('CustomView', MyCustomView);

module.exports = MyCustomView;

// CustomWrapperContainer.js

class CustomWrapperContainer extends React.Component {

api: Object;
constructor (props: Object) {
super(props);
this.state = {
htmlInjection: '',
myDataA: 'Some placeholder text'
};

this.api = RestApi.create();
}

render () {
return (
<View style={styles.container}>
<KeyboardAvoidingView behavior='position'>
<Text>{this.state.myDataA}</Text>
<MyCustomView
style={styles.myStyle}
htmlInjection={this.state.htmlInjection}
onEventA={this.handleEventA.bind(this)}
onEventB={this.handleEventB.bind(this)}
/>
</KeyboardAvoidingView>
</View>
)
}

handleEventA = (data) => {
console.log('on Event A', data);
this.setState({myDataA: data})
};

handleEventB = (error) => {
console.log('On Event B', error);
};
}

const mapStateToProps = (state) => {
return {
}
}

const mapDispatchToProps = (dispatch) => {
return {
}
}

export default connect(mapStateToProps, mapDispatchToProps)(CustomWrapperContainer)

我已经关注了 example来自 React Native 本身以及其他几个,但到目前为止我还没有运气让事件从 iOS 传递到 React Native。我也未能从现有文章中找到对此事的重要帮助。

Ignite 使用 react 版本 15.3.2。也许这就是问题所在?还是那里其他依赖项的版本?我不确定。如果有任何帮助或线索,我将不胜感激。

P.S.:我一直在运行 iOS 9.210.0 的设备和模拟器上运行它,我没有看到任何行为变化,所以这不是问题。

最佳答案

我遇到了一个与 RCTBublingEventBlock 不同的问题。所有 RCTBublingEventBlock 都必须以 on 为前缀。目前文档中未注明这一点。

例如:

onMyEvent //will work
myEvent //no good

关于javascript - React Native UI 组件 : RCTBubblingEventBlock/RCTDirectEventBlock do not seem to work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40931230/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com