You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native(Expo)中Firebase onAuthStateChanged监听器放置位置咨询

Fixing Firebase Auth State Listener Not Triggering on Sign-Out in Expo React Native

Hey there! Let's break down why your auth state listener isn't firing when you sign out, and how to fix it properly.

First, a quick clarification: firebase.auth().onAuthStateChanged is a persistent listener—it should trigger every time the auth state changes (including sign-out) as long as the listener is active and the component is mounted. The issue you're seeing isn't about the placement of the listener in componentDidMount (that's actually the right spot!), but about managing the listener's lifecycle correctly.

Here's the step-by-step fix:

  1. Save the unsubscribe function
    When you register the listener, it returns an unsubscribe function. You need to store this so you can clean up the listener later, which prevents memory leaks and ensures old listeners don't interfere with new ones.

  2. Clean up the listener on component unmount
    When your component is removed from the screen, you should disconnect the listener to avoid unnecessary background activity.

Here's the corrected code example:

class YourComponent extends React.Component {
  componentDidMount() {
    // Save the unsubscribe function returned by onAuthStateChanged
    this.authUnsubscribe = firebase.auth().onAuthStateChanged(user => {
      if (user) {
        console.log("logged in");
      } else {
        console.log("not logged in"); // This will now fire when you sign out!
      }
    });
  }

  componentWillUnmount() {
    // Clean up the listener when the component unmounts
    if (this.authUnsubscribe) {
      this.authUnsubscribe();
    }
  }

  // Properly handle sign-out with async/await to catch errors
  handleSignOut = async () => {
    try {
      await firebase.auth().signOut();
      console.log("Sign-out process started");
    } catch (error) {
      console.error("Error during sign-out:", error);
    }
  }

  render() {
    return (
      <Button title="Sign Out" onPress={this.handleSignOut} />
    );
  }
}

Why this works:

  • The listener stays active for as long as the component is mounted, so when you call signOut(), Firebase detects the auth state change and triggers your listener immediately.
  • Cleaning up in componentWillUnmount ensures that if you navigate away from this component, the listener is disconnected—this prevents memory leaks and ensures you don't get unexpected log messages from unmounted components.

Quick troubleshooting if it still doesn't work:

  • Double-check that your signOut() call is succeeding—use try/catch (like in the example) to catch any errors that might be stopping the sign-out process.
  • If you're signing out from a different component than where the listener is registered, make sure the listener's component is still mounted when you click sign-out. If not, consider moving the listener to a higher-level component (like a root navigation container) that stays mounted throughout your app.

内容的提问来源于stack exchange,提问作者Eric Ahn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:32:17