Invariant violation: `new nativeeventemitter()` requires a non-null argument., js engine: hermes

Explanation:

The error message “invariant violation: `new nativeeventemitter()` requires a non-null argument.” is related to using the `NativeEventEmitter` class in the Hermes JavaScript engine.

The `NativeEventEmitter` is a class provided by the React Native library. It allows you to subscribe to and emit native events from native modules. However, in the Hermes JavaScript engine, there is a requirement that the constructor of `NativeEventEmitter` should be called with a non-null argument.

This means that when creating an instance of `NativeEventEmitter`, you need to pass a non-null argument to the constructor. If you fail to provide a non-null argument, you will encounter the mentioned invariant violation error.

Here is an example demonstrating the correct usage of `NativeEventEmitter`:

<script>
  import { NativeEventEmitter, NativeModules } from 'react-native';

  // Assuming you have a native module named "MyNativeModule" which emits events.

  const myNativeModuleEmitter = new NativeEventEmitter(NativeModules.MyNativeModule);

  const eventListener = myNativeModuleEmitter.addListener(
    'myCustomEvent',
    (event) => {
      console.log('Received event:', event);
    }
  );

  // Remember to remove the listener when no longer needed.
  eventListener.remove();
</script>

Same cateogry post

Leave a comment