Skip to content

ReactEventEmitter

罗坤 edited this page Mar 6, 2020 · 1 revision

com.facebook.react.uimanager.events.ReactEventEmitter

register

unregister

receiveEvent

void receiveEvent(int targetTag, String eventName, @Nullable WritableMap event);

常用于UIManager发送事件给JavaScript

  • 例如: 发送事件到指定view tag id Java
// SimpleViewManager
WritableMap event = Arguments.createMap();
event.putString("message", "messageData");

ReactContext reactContext = (ReactContext)this.getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                this.getId(),
                "topChange",
                event
        );
<CustomView 
  onChange={(event)=>{
    let message = event.nativeEvent.message;
    console.log(message)
  }}
/>

注:在ReactContextBaseJavaModule中由于没有view tag id, 常注册全局RCTDeviceEventEmitter监听事件来发送事件给JavaScript

  • java:
this.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("MODULE_Event_Manager",data);
  • javascript:
// describe
DeviceEventEmitter.addListener('MODULE_Event_Manager',this.handleListenMODULE,this);

// unsubscribe
DeviceEventEmitter.removeAllListeners('MODULE_Event_Manager',this.handleListenMODULE,this);

receiveTouches

getEventEmitter