简体   繁体   English

在 Redux 中,在单个操作中调度多个减速器和案例是一种好习惯吗?

[英]Is it a good practice in Redux to dispatch multiple reducers and cases in a single action?

I have two reducers: errorReducer and userReducer .我有两个减速器: errorReduceruserReducer

I have a login action:我有一个login操作:

export const login = (login) => (dispatch) => {
    axios.post("http://localhost:5000/login", login).then((res) => {
        if (res.data.token) {
            localStorage.setItem("token", res.data.token);
            dispatch({
                type: LOGIN_SUCCESS,
                payload: res.data,
            });
            dispatch({
                type: CLEAR_ERROR,
            });
        } else if (res.data.status === "Invalid credentials.") {
            dispatch({
                type: ERROR,
                payload: res.data.status,
            });
        }
    });
};

As you can see, if the login fails, there is an error.如您所见,如果登录失败,则会出现错误。 An error message appears for the user.为用户显示一条错误消息。 However, if the login is successful, then I need to clear the error state with another dispatch.但是,如果登录成功,那么我需要使用另一个调度清除错误 state。 Is this a good practice?这是一个好习惯吗? If I don't do this, then the error will stay in the state and that's not good I guess.如果我不这样做,那么错误将留在 state 中,我猜这不好。

And if I login successfully for the first try, the CLEAR_ERROR runs too, I don't want that.如果我第一次尝试成功登录, CLEAR_ERROR运行,我不希望这样。

How can I reset the ERROR case in the login action without dispatching?如何在不调度的情况下重置login操作中的ERROR案例?

I am planning to make a loadingReducer as well, but then I will need to dispatch that too.我也计划制作一个 loadingReducer,但是我也需要调度它。 Isn't that too many?是不是太多了? Is there a way around this?有没有解决的办法? I could put error and loading in the userReducer, but I'm not sure if that's a good practice either.我可以在 userReducer 中输入错误和加载,但我也不确定这是否是一个好习惯。

errorReducer:错误减少器:

import { ERROR, CLEAR_ERROR } from "../actions/types";

const initialState = [];

export default function (state = initialState, action) {
    switch (action.type) {
        case ERROR:
            return action.payload;
        case CLEAR_ERROR:
            return [];
        default:
            return state;
    }
}

userReducer:用户减速器:

import { LOGIN_SUCCESS } from "../actions/types";

const initialState = {
    user: {},
    authenticated: false,
};

export default function (state = initialState, action) {
    switch (action.type) {
        case LOGIN_SUCCESS:
            return {
                ...state,
                loading: false,
                authenticated: true,
                ...action.payload,
            };
        default:
            return state;
    }
}

I would use an effect to show an error message in a toaster for example.例如,我会使用效果在烤面包机中显示错误消息。 Not storing it in the state.不将其存储在 state 中。

If you do want to keep it in the state then you could state that a successful login implies that the error should be empty.如果您确实想将其保留在 state 中,那么您可以 state 成功登录意味着错误应该为空。 So you could handle that in the LOGIN_SUCCESS case.所以你可以在 LOGIN_SUCCESS 情况下处理它。

Your states in both reducers do not match though.但是,您在两个减速器中的状态都不匹配。 They should be the same object.它们应该是相同的 object。 So make a file somewhere that exports the initialState and use that in both reducers.所以在某个地方创建一个文件来导出 initialState 并在两个 reducer 中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM