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

Viewed 37459

I cannot resolve the issue. When application loads react native throw warnings.

WARN  `new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.
WARN  `new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.
6 Answers

This is likely due to the newest version of react-native. A lot of libraries still haven't released a new version to handle these warnings (or errors in some case). Examples include https://github.com/react-navigation/react-navigation/issues/9882 and https://github.com/APSL/react-native-keyboard-aware-scroll-view/pull/501. If it bothers you, you can hide the warning for now (source):

import { LogBox } from 'react-native';
LogBox.ignoreLogs(['new NativeEventEmitter']); // Ignore log notification by message
LogBox.ignoreAllLogs(); //Ignore all log notifications

I just add two function to main java module:

    // Required for rn built in EventEmitter Calls.
    @ReactMethod
    public void addListener(String eventName) {

    }

    @ReactMethod
    public void removeListeners(Integer count) {

    }

Example: for fix warning in react-native-fs add functions to android/src/main/java/com/rnfs/RNFSManager.java file.

For Kotlin use this code:

@ReactMethod
fun addListener(type: String?) {
    // Keep: Required for RN built in Event Emitter Calls.
}

@ReactMethod
fun removeListeners(type: Int?) {
    // Keep: Required for RN built in Event Emitter Calls.
}

same error.

change

const eventEmitter = new NativeEventEmitter(NativeModules.CustomModule);

to

const eventEmitter = new NativeEventEmitter();

works

1- update your react-native-reanimated library to "react-native-reanimated": "2.0.0"

2- You should update the babel.config.json file and add react-native-reanimated/plugin to plugins

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    "react-native-reanimated/plugin",
  ],
};

This will fix your issue

It is the issue related with react native reanimated library. I solve it by uninstalling the library and reinstalling it. Remove all the installation steps of react-native-reanimated library provided by https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation.

Simply install library using command npm install react-native-reanimated@2.3.0-beta.1

If the issue still then open project in android studio. Go to file->invalidate cache. After it all things work right.

If you're using @voximplant/react-native-foreground-service, you have to replace node_modules/@voximplant/react-native-foreground-service/index.js by

/*
 * Copyright (c) 2011-2019, Zingaya, Inc. All rights reserved.
 */

'use strict';

import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

const isIOS = Platform.OS === 'ios';
const isAndroid = Platform.OS === 'android';

const ForegroundServiceModule = NativeModules.VIForegroundService;
let EventEmitter;
if (isAndroid) {
    EventEmitter = new NativeEventEmitter(ForegroundServiceModule);
}

/**
 * @property {string} channelId - Notification channel id to display notification
 * @property {number} id - Unique notification id
 * @property {string} title - Notification title
 * @property {string} text - Notification text
 * @property {string} icon - Small icon name
 * @property {number} [priority] - Priority of this notification. One of:
 *                              0 - PRIORITY_DEFAULT (by default),
 *                              -1 - PRIORITY_LOW,
 *                              -2 - PRIORITY_MIN,
 *                              1 - PRIORITY_HIGH,
 *                              2- PRIORITY_MAX
 * @property {string} button - If this property exist, notification will be contain button with text as button value
 */
const NotificationConfig = {

};

/**
 * @property {string} id - Unique channel ID
 * @property {string} name - Notification channel name
 * @property {string} [description] - Notification channel description
 * @property {number} [importance] - Notification channel importance. One of:
 *                                   1 - 'min',
 *                                   2 - 'low' (by default),
 *                                   3 - 'default',
 *                                   4 - 'high',
 *                                   5 - 'max'.
 * @property {boolean} [enableVibration] - Sets whether notification posted to this channel should vibrate. False by default.
 */
const NotificationChannelConfig = {

};


class VIForegroundService {
    static _serviceInstance = null;
    _listeners = new Map();

    /**
     * @private
     */
    constructor() {
        if (isAndroid) {
            EventEmitter.addListener('VIForegroundServiceButtonPressed', this._VIForegroundServiceButtonPressed.bind(this));
        }
    }

    static getInstance() {
        if (this._serviceInstance === null) {
            this._serviceInstance = new VIForegroundService();
        }
        return this._serviceInstance;
    }

    /**
     * Create notification channel for foreground service
     *
     * @param {NotificationChannelConfig} channelConfig - Notification channel configuration
     * @return Promise
     */
    async createNotificationChannel(channelConfig) {
        if (isIOS) {
            console.warn("ForegroundService may be used only Android platfrom.")
            return;
        }
        return await ForegroundServiceModule.createNotificationChannel(channelConfig);
    }

    /**
     * Start foreground service
     * @param {NotificationConfig} notificationConfig - Notification config
     * @return Promise
     */
    async startService(notificationConfig) {
        if (isIOS) {
            console.warn("ForegroundService may be used only Android platfrom.")
            return;
        }
        return await ForegroundServiceModule.startService(notificationConfig);
    }

    /**
     * Stop foreground service
     *
     * @return Promise
     */
    async stopService() {
        if (isIOS) {
            console.warn("ForegroundService may be used only Android platfrom.")
            return;
        }
        return await ForegroundServiceModule.stopService();
    }

    /**
     * Adds a handler to be invoked when button on notification will be pressed.
     * The data arguments emitted will be passed to the handler function.
     *
     * @param event - Name of the event to listen to
     * @param handler - Function to invoke when the specified event is emitted
     */
    on(event, handler) {
        if (isIOS) {
            console.warn("ForegroundService may be used only Android platfrom.")
            return;
        }
        if (!handler || !(handler instanceof Function)) {
            console.warn(`ForegroundService: on: handler is not a Function`);
            return;
        }
        if (!this._listeners.has(event)) {
          this._listeners.set(event, new Set());
        }
        this._listeners.get(event)?.add(handler);
    }

    /**
     * Removes the registered `handler` for the specified event.
     *
     * If `handler` is not provided, this function will remove all registered handlers.
     *
     * @param event - Name of the event to stop to listen to.
     * @param handler - Handler function.
     */
    off(event, handler) {
        if (isIOS) {
            console.warn("ForegroundService may be used only Android platfrom.")
            return;
        }
        if (!this._listeners.has(event)) {
          return;
        }
        if (handler && handler instanceof Function) {
          this._listeners.get(event)?.delete(handler);
        } else {
          this._listeners.set(event, new Set());
        }
    }

    /**
     * @private
     */
     _emit(event, ...args) {
        const handlers = this._listeners.get(event);
        if (handlers) {
            handlers.forEach((handler) => handler(...args));
        } else {
            console.log(`[VIForegroundService]: _emit: no handlers for event: ${event}`);
        }
    }

    /**
     * @private
     */
    _VIForegroundServiceButtonPressed(event) {
        this._emit('VIForegroundServiceButtonPressed', event);
    }
}

export default VIForegroundService;

Solution found here

Related