I am having a little difficulty understanding how Redux-Thunk (or other middleware) execute async actions. From below example, I can see that when onOrder
is called (maybe via a click event), it will dispatch the action created by purchaseBurger
. purchaseBurger
in turn return a function that will dispatch an action indicating purchase started, followed by an http request. My confusion is: When does that function returned by purchaseBurger
actually gets called and executed? How does it get called too?
ContactData.js
const mapDispatchToProps = dispatch => {
return {
onOrder: (orderData) => dispatch(actions.purchaseBurger(orderData))
}
}
orders.js
export const purchaseBurgerStart = (orderData) => {
return {
type: actionTypes.PURCHASE_BURGER_START
}
}
export const purchaseBurger = (orderData) => {
return dispatch => {
dispatch(purchaseBurgerStart());
axios.post('/orders.json', orderData)
.then(response => {
})
.catch(error => {
this.setState({ loading: false });
})
}
}