new nativeemitter() '使用非空参数调用,没有所需的' addListener '方法



我无法解决这个问题。当应用程序加载反应原生抛出警告。

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.

这可能是由于react-native的最新版本。许多库仍然没有发布处理这些警告(或某些情况下的错误)的新版本。例如https://github.com/react-navigation/react-navigation/issues/9882和https://github.com/APSL/react-native-keyboard-aware-scroll-view/pull/501。如果它让您感到困扰,您现在可以隐藏警告(来源):

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

我只是在主java模块中添加了两个函数:

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

示例:用于修复react-native-fs中的警告,将函数添加到android/src/main/java/com/rnfs/RNFSManager.java文件中。

对于Kotlin使用以下代码:

@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.
}

相同的错误

改变
const eventEmitter = new NativeEventEmitter(NativeModules.CustomModule);

const eventEmitter = new NativeEventEmitter();

作品

1-更新你的react-native-reanimated库到"react-native-reanimated"; "2.0.0">

你应该更新babel.config.json并添加react-native-reanimated/plugin插件
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
"react-native-reanimated/plugin",
],
};

这将解决您的问题

世博用户:将@config-plugins/react-native-ble-plx升级到5.0.0版本,警告消失了。

这是与react native reanimated库相关的问题。我通过卸载库并重新安装它来解决它。所提供的react-native-reanimated库的所有安装步骤https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation。

使用命令安装库NPM install react-native-reanimated@2.3.0-beta.1

如果问题仍然存在,那么在android studio中打开项目。转到file->invalidate cache。之后一切正常。

在我的例子中,react-native-location-enabler产生了问题。

刚刚添加了平台检查并执行了所有的"react-native-location-enabler"只针对Android而不针对iOS的代码。

问题解决了。

如果您正在使用@voximplant/react-native- foregroundservice,你必须用

代替node_modules/@voximplant/react-native-foreground-service/index.js
/*
* 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;

此处找到解决方案

相关内容

  • 没有找到相关文章

最新更新