Skip to content

[Android] Move to EventDispatcher instead of RCTEventEmitter #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2024

Conversation

cortinico
Copy link
Contributor

Overview

This is an example of how you should use EventDispatcher rather than RCTEventEmitter for event dispatching on Android (both old and new architecture).

Test Plan

I haven't tested this yet.

@alanleedev
Copy link
Contributor

Thanks @cortinico for working on this. I've tested the code and it seems to work well.

Have tested by creating a test app using this guide: https://gist.github.com/cipolleschi/82b7a9561b8861330efabbd3eb08c6f5
RN version: 0.74.0-rc.9
tested: old architecture, new architecture(bridge + bridgeless)

With the newArchEnabled=true set in gradle.properties, I occasionally see below error without this PR applied:
(cc @Naturalclar )

java.lang.IllegalStateException: Tried to access a JS module before the React instance was fully set up. Calls to ReactContext#getJSModule should only happen once initialize() has been called on your native module.
	at com.facebook.react.bridge.ReactContext.getJSModule(ReactContext.java:180)
	at com.reactnativemenu.MenuView.prepareMenuItem$lambda$0(MenuView.kt:174)
	at com.reactnativemenu.MenuView.$r8$lambda$zm-y_PJLhF-4MEW-E0Gpwcuw7es(Unknown Source:0)
	at com.reactnativemenu.MenuView$$ExternalSyntheticLambda2.onMenuItemClick(Unknown Source:4)
	at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
	at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:935)
	at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:925)
	at com.android.internal.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
	at android.widget.AdapterView.performItemClick(AdapterView.java:330)
	at android.widget.AbsListView.performItemClick(AbsListView.java:1265)
	at android.widget.AbsListView$PerformClick.run(AbsListView.java:3278)
	at android.widget.AbsListView$3.run(AbsListView.java:4244)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:205)
	at android.os.Looper.loop(Looper.java:294)
	at android.app.ActivityThread.main(ActivityThread.java:8177)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

@Naturalclar Naturalclar marked this pull request as ready for review April 20, 2024 22:49
@Naturalclar Naturalclar self-requested a review as a code owner April 20, 2024 22:49
@Naturalclar Naturalclar merged commit 3f7d308 into react-native-menu:master Apr 21, 2024
Copy link
Member

@Naturalclar Naturalclar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants