I'm implementing firebase auth in a react web app with react-router.
A user signs in (at /signin) with either Facebook or Google using the popup sign in, then if successful I route to the main app (/). In the main app component I listen for an auth state change:
componentWillMount() {
this.authListener = this.authListener.bind(this);
this.authListener();
}
authListener listens for the auth change:
authListener() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user changed..', user);
this.setState({
User: {
displayName: user.displayName
}
});
} else {
// No user is signed in.
browserHistory.push('/signin');
}
});
}
Everything works fine, except when I sign out (and go back to /signin) and sign in again using facebook or google. Then I get an error saying:
Warning: setState(...): Can only update a mounted or mounting component.
I suspect that the onAuthStateChanged listener from the now unmounted previous logged in state app is still running.
Is there a way to remove the onAuthStateChanged listener when the App component unmounts?