Javascript 尝试调用componentWillUnmount-NATIVE中的函数

Javascript 尝试调用componentWillUnmount-NATIVE中的函数,javascript,react-native,Javascript,React Native,我正在构建音乐播放器,当用户关闭应用程序时,我运行此组件将卸载: mounted = false; componentDidMount() { this.mounted = true; if (this.mounted) { AppState.addEventListener("change", this.hola); TrackPlayer.addEventListener("playback-track-chan

我正在构建音乐播放器,当用户关闭应用程序时,我运行此组件将卸载:

mounted = false;

componentDidMount() {
    this.mounted = true;
    if (this.mounted) {
        AppState.addEventListener("change", this.hola);
        TrackPlayer.addEventListener("playback-track-changed", this.get_actual_track);
        TrackPlayer.addEventListener("playback-queue-ended", this.get_queue_option);
        this.init();
    }
}

componentWillUnmount() {
    this.mounted = false;
    TrackPlayer.pause().then(() => {
        TrackPlayer.destroy();
    })
}
它关闭应用程序并销毁播放器,但当我重新打开应用程序时,它会向我抛出以下错误:

[Sun Dec 20 2020 13:27:55.491]  ERROR    Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

如何修复它?

您还有一个AppState事件侦听器。另外,也许你可以在暂停和销毁TrackPlayer之前尝试取消侦听(因为这似乎是异步的,并且可能需要一段时间,在卸载组件之后。首先同步取消侦听可能会帮助你摆脱卸载后的状态更改)不,删除eventlistener然后暂停和销毁不起作用。您还有一个AppState事件侦听器。另外,您可以在暂停和销毁TrackPlayer之前尝试取消侦听(因为这似乎是异步的,可能需要一段时间,在卸载组件之后。首先同步取消侦听可能有助于您摆脱卸载后的状态更改)不,删除eventlistener,然后暂停和销毁也不起作用