wonday / react-native-orientation-locker

A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.
MIT License
752 stars 273 forks source link

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

Open Nesh108 opened 2 years ago

Nesh108 commented 2 years ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch react-native-orientation-locker@1.5.0 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.

kesteer commented 1 year ago

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 = {};
Nesh108 commented 1 year ago

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 πŸ˜ƒ