React Native Firebase onStateChanged doesn't unsubscribe on unmount

Viewed 228

I'm using React-Navigation 5 and React Native Firebase libraries. I want onStateChange not to work after navigating from Splash Screen to another screen, but what could be the reason it still works on background?

Here is onStateChange function:

 function onAuthStateChanged(user) {
    setUser(user);
    if (initialized) setinitialized(false);
}

Here is useEffect function:

   useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber; // unsubscribe on unmount
}, []);
1 Answers

I solved it with useFocusEffect from React Navigation. Just replace useEffect with useFocusEffect.

Related