使用非空参数调用 new NativeEventEmitter()` ,而没有所需的 `addListener` 方法

Muh*_*hir 75 reactjs react-native

我无法解决这个问题。当应用程序加载时,本机会抛出警告。

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.
Run Code Online (Sandbox Code Playgroud)

any*_*nya 47

这可能是由于最新版本的react-native 造成的。许多库仍然没有发布新版本来处理这些警告(或在某些情况下的错误)。示例包括https://github.com/react-navigation/react-navigation/issues/9882https://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
Run Code Online (Sandbox Code Playgroud)

  • 这只是消除问题,而不是解决问题 (24认同)
  • 只需要第 2 行“ignoreLogs”。**更新第 2 行**:- `LogBox.ignoreLogs(['new NativeEventEmitter()']);`。编辑队列已满...我无法建议编辑更改。请更新 (2认同)

Saj*_*eed 27

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

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

    }

    @ReactMethod
    public void removeListeners(Integer count) {

    }
Run Code Online (Sandbox Code Playgroud)

示例:修复将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.
}
Run Code Online (Sandbox Code Playgroud)

  • 我不知道 (5认同)

小智 6

同样的错误。

改变

const eventEmitter = new NativeEventEmitter(NativeModules.CustomModule);
Run Code Online (Sandbox Code Playgroud)

const eventEmitter = new NativeEventEmitter();
Run Code Online (Sandbox Code Playgroud)

作品

  • 通过额外的支持信息可以改进您的答案。请[编辑]添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (13认同)
  • 删除该参数将**破坏** iOS 上的事件发射器!从文档中:“这在 IOS 上是必需的,如果未定义,则会引发不变错误。” https://github.com/facebook/react-native/blob/bae0016dc900b5465d0e714c8d467c1422db9eae/packages/react-native/Libraries/EventEmitter/NativeEventEmitter。 d.ts#L40-L41 (2认同)

Muh*_*hir 1

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

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

如果问题仍然存在,则在 android studio 中打开项目。转到文件->使缓存无效。之后一切正常。