3 回答

TA貢獻1828條經驗 獲得超3個贊
感覺這種情況可以直接使用 promise-middleware 中間件,可以直接這樣寫:
// 假設你有兩個 action 發送請求:fetchInfo, fetchDetail
dispatch(fetchInfo())
.then((action) => {
// action.payload 就是 fetchInfo 的請求數據
return dispatch(fetchDetail());
})
.then((action) => {
// action.payload 就是 fetchDetail 的請求數據
})

TA貢獻1815條經驗 獲得超10個贊
function createTodo (todo) {
return (dispatch) => {
return fetch('/todos/create', {})
.then(result => {
dispatch({type: 'CREATED', result})
})
}
}
function getTodo (id) {
return (dispatch) => {
return fetch(`/todos/${id}`, {})
.then(result => {
dispatch({type: 'LOADED', result})
})
}
}
也可以createTodo().then(if(state==='成功!') getTodo())
由于then無論成功失敗都會執行。fetch 尿性。所以dispatch后需要判斷你改變的state滿足了需求,說明createTodo 執行成功了,就執行下一個
這種情況當兩個方法不在同一文件中,或者還會跟隨者其他的操作的時候用
- 3 回答
- 0 關注
- 932 瀏覽
添加回答
舉報