【问题标题】:Firebase onAuthStateChanged and Redux stateFirebase onAuthStateChanged 和 Redux 状态
【发布时间】:2019-04-21 20:44:44
【问题描述】:

我在 React Native 中有一个加载屏幕组件。每当身份验证状态发生变化时,我都会导航到所需的屏幕。 当用户正在注册时,我将 redux 状态标记为 isRegistering = true。这是因为在使用 firebase 完成注册后,我仍然想显示注册完成屏幕并根据身份验证状态推迟加载屏幕的自动导航。

问题似乎是当在auth change上调用事件时,redux状态为空。这就是我从调试中知道的事件,redux 状态之前有数据,并且它在事件之后有数据。但是在事件期间并在 Redux 中检查 isRegistration 时,状态完全为空(重置为默认状态)。

我检查了我的所有操作,reducer 等。没有任何调用,redux 声明只是在 authstatechanged 事件发生时被检查,当时它没有数据,即使数据之前就在那里它。

componentDidMount() {
        firebase.auth().onAuthStateChanged(user => {            
            if (this.props.userObject.isRegistering === false) {                
                this.props.navigation.navigate(user ? 'MainScreen' : 'LandingPage')
            }
        })
    }

const mapStateToProps = (state) => {   
    return {
        userObject: state.user
    }
}

【问题讨论】:

  • 如果你已经在使用 redux,为什么不在 action/reducers 中而不是在组件本身中处理 action?
  • 另外,您是否真的检查过您的this 是否引用了该组件?

标签: javascript firebase react-native redux firebase-authentication


【解决方案1】:

试试这个,

componentDidMount() {
     var that = this;
        firebase.auth().onAuthStateChanged(user => {            
            if (that.props.userObject.isRegistering === false) {                
                that.props.navigation.navigate(user ? 'MainScreen' : 'LandingPage')
            }
        })
    }

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 2021-08-27
    • 2019-01-06
    • 2020-11-15
    • 2016-12-29
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多