2017-08-20 32 views
0

我一直把我的auth.onAuthStateChange().then(user => ...放在頂層React組件的componentDidMount()之內。React Mobx Firebase.onAuthStateChanged監聽器

然後我會刪除的componentWillUnmount()

內監聽我的問題是如何將我mobx-IFY呢?我的想法是這樣的:

class Store { 
    @observable user = null 
    @action killFirebaseListener = this.removeListener() 
    constructor() { 
    this.removeListener = firebase.auth().onAuthStateChange(user => { 
     if (user) this.user = user 
    }) 
    } 
} 

我會再調用從頂層容器組件的componentWillUnmountkillFirebaseListener行動......,只是使用的用戶可觀察在必要。我的理解是,當我的用戶observable在成功登錄或註銷時更新時,我的所有偵聽器都會更新並相應地觸發重新渲染......我對此有錯嗎?

有沒有人有過使用mobx這種「用戶監聽器」的經驗?有沒有人有任何指針或資源,他們可以傳遞。

回答

1

好的。看起來我的想法很好。我加

class Store { 
    @observable user = null 
    constructor() { 
    firebase.auth().onAuthStateChanged(user => { 
     if (user) { 
     this.user = user 
     } 
    }) 
    } 
} 

和監聽程序工作得很好/更新沒有問題。