react-native-orientation-locker icon indicating copy to clipboard operation
react-native-orientation-locker copied to clipboard

Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.

Open Nesh108 opened this issue 2 years ago • 2 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-orientation-locker/src/orientation.android.js b/node_modules/react-native-orientation-locker/src/orientation.android.js
index 8912a1b..c5e0e30 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.android.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.android.js
@@ -8,8 +8,7 @@
 
 "use strict";
 const OrientationNative = require("react-native").NativeModules.Orientation;
-const { NativeEventEmitter } = require("react-native");
-let LocalEventEmitter;
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 
diff --git a/node_modules/react-native-orientation-locker/src/orientation.ios.js b/node_modules/react-native-orientation-locker/src/orientation.ios.js
index 60ee688..e0e3860 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.ios.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.ios.js
@@ -8,8 +8,7 @@
 
 "use strict";
 const OrientationNative = require("react-native").NativeModules.Orientation;
-const { NativeEventEmitter } = require("react-native");
-const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 
diff --git a/node_modules/react-native-orientation-locker/src/orientation.windows.js b/node_modules/react-native-orientation-locker/src/orientation.windows.js
index e281366..e195fb1 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.windows.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.windows.js
@@ -1,7 +1,6 @@
 "use strict";
 const OrientationNative = require("react-native").NativeModules.OrientationLocker;
-const { NativeEventEmitter } = require("react-native");
-const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 

This issue body was partially generated by patch-package.

Nesh108 avatar Jul 19 '22 08:07 Nesh108

This implementation will likely cause issues on Android since you've replaced let LocalEventEmitter with a const and removed NativeEventEmitter from being imported.

LocalEventEmitter = LocalEventEmitter ?? new NativeEventEmitter(OrientationNative);

NativeEventEmitter is not found and therefore pops an error. You're also potentially trying to redefine a const variable.

I suggest this patch file instead since we don't really need NativeEventEmitter anymore.

diff --git a/node_modules/react-native-orientation-locker/src/orientation.android.js b/node_modules/react-native-orientation-locker/src/orientation.android.js
index 8912a1b..c2120e2 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.android.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.android.js
@@ -8,8 +8,7 @@
 
 "use strict";
 const OrientationNative = require("react-native").NativeModules.Orientation;
-const { NativeEventEmitter } = require("react-native");
-let LocalEventEmitter;
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 
@@ -84,8 +83,6 @@ export default class Orientation {
 
   static addOrientationListener = (cb) => {
     var key = getKey(cb);
-    LocalEventEmitter =
-      LocalEventEmitter ?? new NativeEventEmitter(OrientationNative);
     listeners[key] = LocalEventEmitter.addListener(
       "orientationDidChange",
       (body) => {
@@ -105,8 +102,6 @@ export default class Orientation {
 
   static addDeviceOrientationListener = (cb) => {
     var key = getKey(cb);
-    LocalEventEmitter =
-      LocalEventEmitter ?? new NativeEventEmitter(OrientationNative);
     listeners[key] = LocalEventEmitter.addListener(
       "deviceOrientationDidChange",
       (body) => {
@@ -126,8 +121,6 @@ export default class Orientation {
 
   static addLockListener = (cb) => {
     var key = getKey(cb);
-    LocalEventEmitter =
-      LocalEventEmitter ?? new NativeEventEmitter(OrientationNative);
     listeners[key] = LocalEventEmitter.addListener("lockDidChange", (body) => {
       cb(body.orientation);
     });
diff --git a/node_modules/react-native-orientation-locker/src/orientation.ios.js b/node_modules/react-native-orientation-locker/src/orientation.ios.js
index 60ee688..e0e3860 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.ios.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.ios.js
@@ -8,8 +8,7 @@
 
 "use strict";
 const OrientationNative = require("react-native").NativeModules.Orientation;
-const { NativeEventEmitter } = require("react-native");
-const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 
diff --git a/node_modules/react-native-orientation-locker/src/orientation.windows.js b/node_modules/react-native-orientation-locker/src/orientation.windows.js
index e281366..e195fb1 100644
--- a/node_modules/react-native-orientation-locker/src/orientation.windows.js
+++ b/node_modules/react-native-orientation-locker/src/orientation.windows.js
@@ -1,7 +1,6 @@
 "use strict";
 const OrientationNative = require("react-native").NativeModules.OrientationLocker;
-const { NativeEventEmitter } = require("react-native");
-const LocalEventEmitter = new NativeEventEmitter(OrientationNative);
+const LocalEventEmitter = require('react-native').DeviceEventEmitter;
 
 var listeners = {};
 

kesteer avatar Jan 11 '23 04:01 kesteer

Hello @kesteer, thank you very much for your reply!

I personally haven't seen any errors/warnings but your version makes much more sense. I will follow your patch as it does seem better 😃

Nesh108 avatar Jan 11 '23 09:01 Nesh108