设置 BEGIN_API_CALL action.
import * as types from "./actionTypes";
export function beginApiCall() {
return { type: types.BEGIN_API_CALL };
}
END_API_CALL action 不需要手动设置,因为每个 API call 都会自动发一个尾缀为 _SUCCESS 的 action.
export function loadCourses() {
return function(dispatch) {
dispatch(beginApiCall());
return courseApi
.getCourses()
.then(courses => {
dispatch(loadCourseSuccess(courses));
})
.catch(error => {
throw error;
});
};
}
在每一个 Thunk 里 API CALL 之前添加 dispatch(beginApiCall());
import * as types from "../actions/actionTypes";
import initialState from "./initialState";
function actionTypeEndsInSuccess(type) {
return type.substring(type.length - 8) === "_SUCCESS&