0

我試圖根據文檔使用Firebase身份驗證偵聽器來分派logIn函數。但是我想用偵聽器觸發一個redux動作來填充我的狀態。如何使用React Native從Firebase身份驗證中分發Redux動作

當我嘗試運行下面的代碼,在與火力成功登錄時,出現「無法讀取屬性未定義‘登陸’。

請HALP

class RootApp extends Component { 
 
    componentWillMount() { 
 
     // Listen for Auth Changes 
 
     firebase.auth().onAuthStateChanged(function(user) { 
 
      if (user) { 
 
       this.props.logIn(
 
        uid, 
 
        displayName, 
 
        photoURL, 
 
        providerId, 
 
        email, 
 
        emailVerified 
 
       ); 
 
       // User is signed in. 
 
      } else { 
 
       console.log("Not logged in"); 
 
       // No user is signed in. 
 
      } 
 
     }); 
 
    } 
 

 
    render() { 
 
     if (this.props.auth.checking == true) { 
 
      return (
 
       <ActivityIndicator 
 
        animating={true} 
 
        style={styles.activityIndicator} 
 
        size="large" 
 
       /> 
 
      ); 
 
     } else if (this.props.auth.signedIn == true) { 
 
      return <SignedIn />; 
 
     } else { 
 
      return <SignedOut />; 
 
     } 
 
    } 
 
} 
 

 
const styles = StyleSheet.create({ 
 
    container: { 
 
     flex: 1, 
 
     backgroundColor: "#fff", 
 
     alignItems: "center", 
 
     justifyContent: "center", 
 
     fontSize: 96 
 
    }, 
 
    activityIndicator: { 
 
     flex: 1, 
 
     justifyContent: "center", 
 
     alignItems: "center", 
 
     height: 80 
 
    } 
 
}); 
 

 
const mapStateToProps = state => { 
 
    return { 
 
     auth: state.auth 
 
    }; 
 
}; 
 

 
const mapDispatchToProps = dispatch => { 
 
    return { 
 
     logIn: (
 
      uid, 
 
      displayName, 
 
      photoURL, 
 
      providerId, 
 
      email, 
 
      emailVerified 
 
     ) => { 
 
      dispatch(
 
       logIn(
 
        uid, 
 
        displayName, 
 
        photoURL, 
 
        providerId, 
 
        email, 
 
        emailVerified 
 
       ) 
 
      ); 
 
     } 
 
    }; 
 
}; 
 

 
export default connect(mapStateToProps, mapDispatchToProps)(RootApp);

回答

0

對於其他攻擊者來說,答案是將firebase示例偵聽器函數轉換爲胖箭頭函數,因爲在他們的示例中使用的舊式函數將它自己的「this」綁定,其中的箭頭函數不行。

因此,這:

firebase.auth()onAuthStateChanged(功能(用戶){

需要改變至:

firebase.auth()onAuthStateChanged(用戶=> {