Skip to content
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

new NativeEventEmitter() was called with a non-null argument without the required addListener method. #137

Open
Arnab2021 opened this issue Nov 2, 2021 · 3 comments

Comments

@Arnab2021
Copy link

Hi

I am using this package to get the WIFI state.
When I am running the App I am getting this error -
new NativeEventEmitter() was called with a non-null argument without the required addListener method.
new NativeEventEmitter() was called with a non-null argument without the required addListener method.

image

I have all the installation process and the coing standard as per the document.

Please help me to get rid of that.

Megatron4537 added a commit to Megatron4537/react-native-system-setting that referenced this issue Nov 8, 2021
@geroale
Copy link

geroale commented Mar 9, 2022

Same issue here

@willyhorizont
Copy link

I have this message before but I don't see it again after I change my code to

`
// utils.js
import { locationAction } from '../slices/locationSlice';

export const listenLocationChange = async (SystemSetting, dispatch) => {
return await SystemSetting.addLocationListener((enable) => {
dispatch(locationAction.setIsLocationEnabled(enable));
});
};

// Components.js
useEffect(() => {
const locationListener = listenLocationChange(SystemSetting, dispatch);
return () => {
if (typeof locationListener?.remove === 'function') SystemSetting.removeListener(locationListener);
};
}, []);
`

I hope you already solve your problem

@GabLeRoux
Copy link

GabLeRoux commented Sep 14, 2023

I worked on this recently and had similar issue. Here's the same comment as above with proper indentation and syntax highlighting.

// utils.js
import { locationAction } from '../slices/locationSlice';

export const listenLocationChange = async (SystemSetting, dispatch) => {
  return await SystemSetting.addLocationListener((enable) => {
    dispatch(locationAction.setIsLocationEnabled(enable));
  });
};
// Components.js
useEffect(() => {
  const locationListener = listenLocationChange(SystemSetting, dispatch);
  return () => {
    if (typeof locationListener?.remove === 'function')
      SystemSetting.removeListener(locationListener);
  };
}, []);

In other word, the fix is to actually add and remove the listeners using SystemSetting.addLocationListener and SystemSetting.removeListener.

Depending on the implementation, these changes could actually be directly in the same component.


Edit 2023-09-19: The library's code could actually be updated according to the following stack overflow post:
https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir

I added the functions to SystemSetting.java and exposed them in SystemSetting.d.ts and it looks like it worked so this could probably be a better solution.

diff --git a/SystemSetting.d.ts b/SystemSetting.d.ts
index 403808c..ab96e4d 100644
--- a/SystemSetting.d.ts
+++ b/SystemSetting.d.ts
@@ -31,6 +31,8 @@ interface VolumeData {
 }
 
 interface SystemSetting {
+  addListener: (eventName: string) => void;
+  removeListeners: (count: number) => void;
   getBrightness: () => Promise<number>;
   setBrightness: (val: number) => Promise<boolean>;
   setBrightnessForce: (val: number) => Promise<boolean>;
diff --git a/android/src/main/java/com/ninty/system/setting/SystemSetting.java b/android/src/main/java/com/ninty/system/setting/SystemSetting.java
index 8ade5e6..c5d3adc 100644
--- a/android/src/main/java/com/ninty/system/setting/SystemSetting.java
+++ b/android/src/main/java/com/ninty/system/setting/SystemSetting.java
@@ -199,6 +199,25 @@ public class SystemSetting extends ReactContextBaseJavaModule implements Activit
         return SystemSetting.class.getSimpleName();
     }
 
+    /**
+     *
+     * @see https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir
+     * @param eventName the name of the event
+     */
+    @ReactMethod
+    public void addListener(String eventName) {
+
+    }
+
+    /**
+     * @see https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir
+     * @param count the number of listeners to remove
+     */
+    @ReactMethod
+    public void removeListeners(Integer count) {
+
+    }
+
     @ReactMethod
     public void setScreenMode(int mode, Promise promise) {
         mode = mode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL ? mode : Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;

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

No branches or pull requests

4 participants