Friday, 27 January 2023

RCTModernEventEmitter fires twice for android fabric component

I am trying to create a fabric component for android, specifically I want to use the onClickHandler of the button component and pass a callback to react-native side via RCTModernEventEmitter. It works fine for iOS but for android the RCTModernEventEmitter emits twice every time I click the button

This is my spec

import type {HostComponent, ViewProps} from 'react-native';
import type {
  DirectEventHandler
} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

type Event = Readonly<{
  text?: string;
}>;

interface NativeProps extends ViewProps {
  text: string;
  onClickHandler?: DirectEventHandler<Event>; ////Event name should start with on
}

export default codegenNativeComponent<NativeProps>(
  'MyButtonView',
) as HostComponent<NativeProps>;

On native side I have created following files

public class MyButtonViewManager extends SimpleViewManager<MyButtonView> {

    public static final String NAME = "MyButtonView";
    ReactApplicationContext mCallerContext;

    public MyButtonViewManager(ReactApplicationContext reactContext) {
        mCallerContext = reactContext;
    }
 
    @NonNull
    @Override
    public String getName() {
        return NAME;
    }

    @NonNull
    @Override
    protected MyButtonView createViewInstance(@NonNull ThemedReactContext reactContext) {
        return new MyButtonView(reactContext);
    }

    @ReactProp(name = "text")
    public void setQrCodeText(MyButtonView view, String text) {
        view.setText(text);
    }


    @Nullable
    @Override
    public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.of("topOnClickHandler",
                MapBuilder.of("registrationName", "onClickHandler")
        );
    }

}

public class MyButtonClickEvent extends Event<MyButtonClickEvent> {

    public MyButtonClickEvent(int viewId) {
        super(viewId);
    }
 
    @Override
    public String getEventName() {
        return "topOnClickHandler";
    }

//    @Override
//    public void dispatch(RCTEventEmitter rctEventEmitter) {
//        super.dispatch(rctEventEmitter);
//        rctEventEmitter.receiveEvent(getViewTag(), getEventName(), Arguments.createMap());
//    }

    @Override
    public void dispatchModern(RCTModernEventEmitter rctEventEmitter) {
        super.dispatchModern(rctEventEmitter);
        rctEventEmitter.receiveEvent(-1,
                getViewTag(),getEventName(),
                Arguments.createMap()
        );
    }

    @Nullable
    @Override
    protected WritableMap getEventData() {
        WritableMap event = Arguments.createMap();
        event.putString("message", "MyMessage");
        return event;
    }
}

public class MyButtonView extends androidx.appcompat.widget.AppCompatButton {

    public MyButtonView(Context context) {
        super(context);
        configureViews();
    }

    private void configureViews(){
        setBackgroundColor(Color.YELLOW);
        setOnClickListener(view -> {
            ReactContext reactContext = (ReactContext)getContext();
            EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(
                    reactContext ,getId()
            );
            eventDispatcher.dispatchEvent(new MyButtonClickEvent(getId()));
        });
    }
}

On JS side

 <MyButtonView
          style=
          onClickHandler={(value: any) => {
            console.log('Hello ok bye', value.nativeEvent);
          }}
          text="Hello"
        />

I get value in onClickHandler of MyButtonView twice even though I press the button once

Fullrepo is here https://github.com/PritishSawant/ReactNativeFabricEventListenerExample

Edit:

I have updated my code to 0.71.1 and you can find it here



from RCTModernEventEmitter fires twice for android fabric component

No comments:

Post a Comment