【问题标题】:How to Wait for Dispatch?如何等待发货?
【发布时间】:2016-06-18 18:38:30
【问题描述】:

我正在使用带有 es6 的 react 和 Flux 实用程序。我希望SalesStore 在执行自己的调度之前等待SessionStore 完成。在SalesStore 我有以下内容,但调度不等待。有谁知道问题可能是什么?谢谢。

class SalesStore extends ReduceStore {

    getInitialState() {
        return {};
    }

    reduce(state, action) {
        switch(action.type) {
            case Constants.FETCH_SALES:
                this.getDispatcher().waitFor([SessionStore.getDispatchToken()]);
                return state;

            case Constants.FETCH_SALES_SUCCESS:                
                this.getDispatcher().waitFor([SessionStore.getDispatchToken()]);
                return action.payload;

            case Constants.FETCH_SALES_FAILURE:
                this.getDispatcher().waitFor([SessionStore.getDispatchToken()]);
                return state;

            default:
                return state;
        }
    }

}

export default new SalesStore(AppDispatcher);

【问题讨论】:

    标签: reactjs flux reactjs-flux


    【解决方案1】:

    reducer 必须是纯函数 所以你应该将异步调用转移到动作创建者

    function getToken(){
       // Async call then
       return {
         type:'GET_TOKEN_SUCCESS' 
       }
    }
    
    function getSales(){
       // async then
       return {
         type:'GET_SALES_SUCCESS' 
       }
    }
    
    // main action will call `getToken` and `fetchSales` actions
    function fetchSales(params){
        return dispatch => {
             dispatch(getToken()).then((res)=> dispatch(getSales())
        }
    }
    

    要管理承诺,您可以使用middleware

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2018-11-12
      • 2015-08-24
      • 2013-06-29
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      相关资源
      最近更新 更多