Can anyone please explain how react useEffect hook is working as ComponentWillUnmount here. Also how the loading state is handled using firebase onAuthStateChanged?
const [loading, setLoading] = useState(false);
useEffect(() => {
const auth = getAuth();
const unsubscribe = onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
setLoading(true);
});
return unsubscribe;
}, []);
useEffect will take the function that you return and run it eventually when your component is unmounted. This is indeed the similar to how ComponentWillUnmount works. If you don't want any action on unmount just don't return anything in your useEffect.
You can read more about it in the React effect hooks documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With