Why don't chaining dispatch Redux-Thunk

天大地大妈咪最大 提交于 2019-12-24 19:10:34

问题


Why don't chaining dispatch Redux Thunk? Only work first dispatch and second dispatch is not working.

Store:

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

Action:

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

         return true;
    }
}

Edit It's worked:

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

回答1:


From the 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')
      )
    );

But I would recommend using redux-saga instead of redux-thunk because of these reasons.



来源:https://stackoverflow.com/questions/50208749/why-dont-chaining-dispatch-redux-thunk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!