【问题标题】:Why are my redux actions not firing correctly?为什么我的 redux 操作没有正确触发?
【发布时间】:2020-05-22 05:42:16
【问题描述】:

我正在尝试使用 redux 和 firebase 实施身份验证检查并登录/注销用户。我有以下代码:
动作类型:

export const LOGIN_REQ = 'AUTH_REQ';
export const LOGOUT_REQ = 'LOGOUT_REQ';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_FAILED = 'AUTH_FAILED';
export const GET_AUTH = 'GET_AUTH';

减速器:

import * as ActionTypes from './ActionTypes';
export const auth = (state = {
    isAuth: false,
    user: null
}, action) => {
    switch (action.type) {
        case ActionTypes.LOGIN_REQ:
            return { ...state, isAuth: false, user: null };
        case ActionTypes.LOGOUT_REQ:
            return { ...state, isAuth: false, user: null };
        case ActionTypes.AUTH_FAILED:
            return { ...state, isAuth: false, user: null };
        case ActionTypes.AUTH_SUCCESS:
            return { ...state, isAuth: true, user: action.payload };
        case ActionTypes.GET_AUTH:
            return state;
        default:
            return state;
    }
}

感谢:

export const getAuth = () => (dispatch) => {
    firebase.auth().onAuthStateChanged((user) => {
        if (user) {
            console.log('Get AUTH called');
            dispatch(authSuccess());
        }
        else {
            console.log('Get AUTH called');
            dispatch(authFailed());
        }
    });
}

export const loginReq = (email, password, remember) => (dispatch) => {
    firebase.auth().signInWithEmailAndPassword(email, password)
        .then((cred) => {
            if (remember === false) {
                firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE);
                console.log('Logged In with Redux without persist');
            }
            else {
                console.log('Logging in with Persist');
            }
            console.log('Dispatching Success !');
            dispatch(authSuccess(cred.user.uid));
        })
        .catch((err) => {
            console.log(err);
            dispatch(authFailed(err));
        });
}

export const logoutReq = () => (dispatch) => {
    firebase.auth().signOut()
        .then(() => dispatch(getAuth()))
        .catch((err) => console.log(err));
}

export const authSuccess = (uid = null) => ({
    type: ActionTypes.AUTH_SUCCESS,
    payload: uid
});

export const authFailed = (resp) => ({
    type: ActionTypes.AUTH_FAILED,
    payload: resp
});

我从一个组件调用它,如下所示:

const mapStateToProps = state => {
    return {
        isAuth: state.isAuth,
        user: state.user
    }
}

const mapDispatchToProps = dispatch => ({
    getAuth: () => { dispatch(getAuth()) },
    loginReq: (email, password, remember) => { dispatch(loginReq(email, password, remember)) },
    logoutReq: () => { dispatch(logoutReq()) }
})  
handleLogin() {
       this.props.loginReq(this.state.email, this.state.password, this.state.remember);
  }
handleLogOut() {
        this.props.logoutReq();
    }
<BUTTON onClick=()=>this.handleLogOut()/handleLogin()>

我快要哭了,因为我无法弄清楚为什么我的 loginReq 会触发一个或多个 gitAuth() 方法,即使我单击了一次按钮。这只发生在 loginReq() 动作中。我没有指定 loginReq() 应该触发它的任何地方。 我还在主屏幕的组件中调用了 getAuth() 方法,该方法在应用程序启动时检查身份验证状态一次。
编辑:我在组件中登录了控制台,并在主组件中安装了方法,所以我知道这个 getAuth() 调用不是来自那里。

【问题讨论】:

    标签: firebase react-native react-redux redux-thunk


    【解决方案1】:

    我的一个朋友告诉我,这与 firebase 提供的 onAuthStateChange() 中的“观察者”有关。基本上,我手动认为用户已通过身份验证与观察者这样做之间存在冲突。

    【讨论】:

      【解决方案2】:

      Imo 答案做得不好,尝试更好地重组它,你所谓的“Thunk”实际上是“Actions”。但是,如果我要告诉您一些可能有帮助的事情,那么问题可能出在 thunk 中间件配置或调度程序处理 firebase 的方式上,所以我会说您最好尝试使用 react-redux 编写一个 apporach -firebase 库(这个:http://react-redux-firebase.com/docs/getting_started)它可以更轻松地将 redux 与 firebase 后端连接起来。其他很好的参考,我从中学到的,是 The Net Ninja 的关于 react、redux 和 firebase 的教程播放列表。

      【讨论】:

      • 我正在使用 expo sdk 来构建它,当我查看 react-redux-firebase 的页面时,它说他们不支持 expo 托管工作流。我现在不想进入一个简单的工作流程。我将把这个练习留到以后。事实证明,我可以通过简单地调用它来停止倾听观察者。
      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 2016-06-14
      • 2020-08-23
      • 2021-07-03
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多