-
StackOverflow 文档
-
redux 教程
-
异步数据流
-
使用带有 Promises 的 redux-thunk
import 'whatwg-fetch';
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
}
function parseJSON(response) {
return response.json();
}
function getJSON(endpoint, params) {
return fetch(endpoint, params)
.then(checkStatus)
.then(parseJSON);
}
export function action() {
return dispatch => getJSON('/example-endpoint')
.then((result) => {
dispatch({
type: GET_SUCCESS,
result,
});
})
.catch((error) => {
dispatch({ type: GET_FAILURE, error });
});
}