为什么不链式调度 Redux-Thunk [英] Why don't chaining dispatch Redux-Thunk
本文介绍了为什么不链式调度 Redux-Thunk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不链式调度 Redux Thunk?只有第一次派遣工作,第二次派遣无效.
商店:
const store = createStore(reducers, loadState(), applyMiddleware(thunk));
操作:
export function doSomething(name) {返回函数(调度){派遣({类型:'用户名',有效载荷:名称})派遣({类型:'OTHER_TYPE',有效载荷:'文本'})返回真;}}
编辑成功了:
return Promise.all([派遣({类型:'用户名',有效载荷:名称}),派遣({类型:'OTHER_TYPE',有效载荷:'文本'})])
解决方案
来自 docs:
//我们可以分派普通对象操作和其他 thunk,//这让我们可以在单个流中组合异步操作.退货发货(makeASandwichWithSecretSauce('我的奶奶')).then(() =>Promise.all([调度(makeASandwichWithSecretSauce('我')),调度(makeASandwichWithSecretSauce('我的妻子'))])).then(() =>dispatch(makeASandwichWithSecretSauce('我们的孩子'))).then(() =>dispatch(getState().myMoney > 42 ?取款(42):道歉('我','三明治店')));
<块引用>
但我建议使用 redux-saga 而不是 redux-thunk因为这些原因.
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'
})
])
解决方案
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.
这篇关于为什么不链式调度 Redux-Thunk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文