Onyx
Onyx

Reputation: 5782

How to unsubscribe from a DeviceEventEmitter eventListener?

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

Upvotes: 0

Views: 256

Answers (0)

Related Questions