0

I have the following useEffect hook that adds an event listener if isAuthenticated is true:

useEffect(() => {
    if (isAuthenticated) {
        Doorman.guard(onSessionExpiration);
    }
}, [isAuthenticated, onSessionExpiration]);

The problem is that when the user logs out, the Doorman.guard(onSessionExpiration) is not unregistered which causes some problems, which is why I'm trying to figure out how to unsubscribe.

The guard function looks like this:

import { DeviceEventEmitter } from 'react-native';

export const Doorman = {
    guard(fn: () => void) {
        return DeviceEventEmitter.addListener('UNAUTHORIZED', fn);
    }
};
Onyx
  • 5,186
  • 8
  • 39
  • 86
  • Does this answer your question? [react native DeviceEventEmitter unsubscribe from event](https://stackoverflow.com/questions/58285364/react-native-deviceeventemitter-unsubscribe-from-event) – Sergiu Paraschiv Sep 13 '22 at 13:03
  • @SergiuParaschiv No because I have to use DeviceEventEmitter and the answer in the question you've supplied uses NativeEventEmitter. – Onyx Sep 13 '22 at 13:12
  • There's a comment there answering your question. Yours is still an exact duplicate of that one. – Sergiu Paraschiv Sep 13 '22 at 13:25

0 Answers0