在jQuery.ajax上使用redux-thunk
示例
const loadUser = userId => dispatch => {
dispatch({ type: 'USER_LOADING' });
$.ajax('/users/' + userId, {
type: 'GET',
dataType : 'json'
}).done(response => {
dispatch({ type: 'USER_LOADED', user: response });
}).fail((xhr, status, error) => {
dispatch({ type: 'USER_LOAD_ERROR', status, error });
});
};要使用,请像其他任何动作创建者一样调度:
store.dispatch(loadUser(123));
这将导致USER_LOADING调度初始动作,该初始动作可用于显示加载指示符(如果需要的话),并且在收到响应后,将根据请求的结果调度USER_LOADED动作或USER_LOAD_ERROR动作$.ajax。