首页 > 解决方案 > 将 Promise 传递给 reducer 并在那里解决它是一件坏事吗?

问题描述

我正在做我的爱好项目,它是用 React、Redux 和 Redux-thunk 制作的。

我现在有 3 个减速器:

在我的动作创建器中,我必须按特定顺序调度多个动作。我需要将某种数据从第二个动作传递给第三个动作。所以我这样做了:

Actions.js

export const deleteAttacker = id => {
    return (dispatch) => {
        dispatch({
            type: DELETE_ATTACKER,
            payload: {
                id
            }
        });

        updateDefenders(id, dispatch)
            .then(data => dispatch({
                type: COMBINATIONS_DELETE_ATTACKER,
                payload: {
                    id,
                    data,
                }
            }));
    };
};

export const updateDefenders = (id, dispatch) => {
    return new Promise((resolve) => {
        dispatch({
            type: UPDATE_DEFENDERS,
            payload: {
                id,
                resolve
            }
        });
    });
}

Reducers/Defenders.js

...
case UPDATE_DEFENDERS: {
            const data = [];

            ...
            // push new elements into the 'data' array
            // update the newState object

            action.payload.resolve({ data });
            // Return the data array, so I can pass that array to the third action.
            // Is it a bad thing to do...? 

            return {
                ...newState
            };
}
...

我的问题:可以在我的 reducer 中调用 resolve 方法吗?或者这是一件坏事,我应该以不同的方式解决我的问题?

感谢您的建议!

标签: reactjsreact-reduxredux-thunk

解决方案


推荐阅读