所以我在我的react-redux应用程序中测试这个api请求:
import $ from 'jquery';
window.$ = $;
const API_KEY = '<api-key>';
const ROOT_URL = `https://api.behance.net/v2/users?client_id=${API_KEY}`;
export const FETCH_USER = 'FETCH_USER';
export function fetchUser(users) {
const request = $.ajax({
url: `${ROOT_URL}&q=${users}`,
type: 'get',
data: { users: {} },
dataType: 'jsonp'
})
.done(response => {
console.log('Request:', request);
})
.fail(error => {
console.log('Ajax request fails');
console.log(error);
});
return {
type: FETCH_USER,
payload: request
};
}然而,在Request:的Chrome控制台中,我得到了一个带有readyState而不是Promise的对象,在这一点上,我甚至需要redux-promise包吗?
发布于 2018-10-19 06:59:35
我知道你想做什么了,但是我不认为发送一个承诺给reducer是一个好主意,相反,我会建议你使用中间件redux-thunk。我会这样重写你的动作
export const fetchUser= ()=>(dispatch,getState)=>{
let params={
method:'get',
body:{ users: {} },
}
fetch( `${ROOT_URL}&q=${users}`, params).then
.then(response => response.json())
.then((response) =>{
dispatch({
type:Const.ON_RESPONSE_OK,
payload:response
})
})
.catch(error => {
dispatch({
type:Const.ON_RESPONSE_ERROR,
payload:Error
})
});
}并重写reducer以处理有效负载
https://stackoverflow.com/questions/52883409
复制相似问题