我使用NativeEventEmitter的addListener方法将JavaScript中的侦听器添加到本机端。
import {NativeModules, NativeEventEmitter, EmitterSubscription} from 'react-native';
const CoreBridge = NativeModules.Core;
class Core extends NativeEventEmitter {
constructor() {
super(CoreBridge);
}
addListener(event: CoreEvent, handler: Function): EmitterSubscription {
return super.addListener(event, handler);
}
}
export default new Core();
我可以在本机Java端处理addListener()吗?我试过这个,但没有被称为:
@ReactMethod
public void addListener(String event) {
//register native listener for event emitting
}
对于iOS,可以这样做:
#pragma mark - RCTEventEmitter
- (NSArray<NSString > )supportedEvents {
return @[EVENT_ONE,
EVENT_TWO];
}
/// This method will be called when the first event listener is added.
- (void)startObserving {}
/// This method will be called when the last event listener is removed.
- (void)stopObserving {}
我可以为Android处理相同的事件侦听器吗?