Redux 和 Redux thunk 不同步

Redux and Redux thunk not synchronous

我使用 redux 的假设是调度操作是同步任务。

fire action 1 - > store updated
fire action 2 -> store updated

在我目前正在进行的一个项目中,我有一个产品定制器,允许一些用户 selection,他们可以下多个订单,但如果他们只订购他们当前的 selection 和 select "purchase",我触发 "addOrder",将它们的 selection 添加到 orders 数组,然后是 "purchase" 动作,这是一个 thunk将存储在 redux 中的订单提交到我的购物车 API.

我曾期望我能够依赖商店处于一致的状态,在每次操作后可靠地进行,因此当第二个操作触发时,它会保持在第一个操作之后的状态常规动作在它之前开火,但没有骰子。

在我连接的组件中,我分派每个动作:

//.... inside component
purchase = () => {
  this.props.addOrder(); // regular action
  this.props.purchase(); // thunk
};
// ... rest of component

是的,调度始终 100% 同步,除非被中间件改变。是的,默认情况下,您可以在调度后再次调用 getState() 以获取更新后的状态:

function checkStateAfterDispatch() {
    return (dispatch, getState) => {
        const firstState = getState();
        dispatch({type : "FIRST_ACTION"});

        const secondState = getState();

        if(secondState.someField != firstState.someField) {
            dispatch({type : "SECOND_ACTION"});
        }    
    }
}