【问题标题】:Why don't chaining dispatch Redux-Thunk为什么不链接调度 Redux-Thunk
【发布时间】:2018-10-16 22:30:28
【问题描述】:

为什么不链接调度 Redux Thunk?只能工作第一次调度,第二次调度不起作用。

商店:

const store = createStore(reducers, loadState(), applyMiddleware(thunk));

行动:

export function doSomething(name) {
    return function (dispatch) {
        dispatch({
            type: 'USERNAME',
            payload: name
        })
        dispatch({
            type: 'OTHER_TYPE',
            payload: 'text'
        })

         return true;
    }
}

编辑 成功了:

return Promise.all([
        dispatch({
            type: 'USERNAME',
            payload: name
        }),
        dispatch({
            type: 'OTHER_TYPE',
            payload: 'text'
        })
    ])

【问题讨论】:

    标签: reactjs redux redux-thunk react-thunk


    【解决方案1】:

    来自docs

        // We can dispatch both plain object actions and other thunks,
        // which lets us compose the asynchronous actions in a single flow.
    
        return dispatch(
          makeASandwichWithSecretSauce('My Grandma')
        ).then(() =>
          Promise.all([
            dispatch(makeASandwichWithSecretSauce('Me')),
            dispatch(makeASandwichWithSecretSauce('My wife'))
          ])
        ).then(() =>
          dispatch(makeASandwichWithSecretSauce('Our kids'))
        ).then(() =>
          dispatch(getState().myMoney > 42 ?
            withdrawMoney(42) :
            apologize('Me', 'The Sandwich Shop')
          )
        );
    

    但我会推荐使用 redux-saga 而不是 redux-thunk 因为these的原因。

    【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2018-12-19
    • 2020-08-18
    • 2020-03-25
    • 2017-05-25
    • 2018-06-25
    • 1970-01-01
    • 2021-04-05
    • 2017-09-16
    相关资源
    最近更新 更多