How to unsubscribe from a DeviceEventEmitter eventListener?

197 views Asked by At

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);
    }
};
0

There are 0 answers