Почему бы не отправить цепочку Redux-Thunk - PullRequest
0 голосов
/ 07 мая 2018

Почему бы не отправить в цепочку 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'
        })
    ])

1 Ответ

0 голосов
/ 07 мая 2018

Из документов :

    // 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 из-за этих причин.

...