1 回答

TA貢獻1859條經驗 獲得超6個贊
您可以將這些操作創建者轉換為async版本,然后您可以等待它們,以便它們按順序執行。
https://medium.com/@gaurav5430/async-await-with-redux-thunk-fff59d7be093
例如在你的fetchData
function fetchData() {
return async (dispatch) => {
const url = 'http://www.boredapi.com/api/activity/'
try{
const res = await fetch(url)
const activity = await res.json();
dispatch({type: "FETCH_DATA", payload: activity})
}
catch (error) {
console.log(error);
}
}
一旦它們出現,async您就可以在您的中等待它們handleSave(一旦您將其轉換為async),這將確保它們按順序被調用。
handleSave = async () => {
await this.props.postData({
name:this.props.activity,
type_name: this.props.type
})
await this.props.fetchList()
await this.props.fetchData()
}
添加回答
舉報