redux thunk未使用fetch进行调度,但使用axios
由于nock与axios兼容,而且moxios没有太多文档,所以我决定使用fetch。但是现在我的行动没有被分派redux thunk未使用fetch进行调度,但使用axios,redux,fetch,redux-thunk,Redux,Fetch,Redux Thunk,由于nock与axios兼容,而且moxios没有太多文档,所以我决定使用fetch。但是现在我的行动没有被分派 import { thunk } from 'redux-thunk'; import axios from 'axios'; import { CURRENT_CONDITION_SUCCESS, CURRENT_CONDITION_ERROR, LOAD, } from './types'; export function initializeLoa
import { thunk } from 'redux-thunk';
import axios from 'axios';
import {
CURRENT_CONDITION_SUCCESS,
CURRENT_CONDITION_ERROR,
LOAD,
} from './types';
export function initializeLoad() {
return (dispatch) => {
dispatch({type: LOAD, bool: true})
fetch("http://api.wunderground.com/api/777ba6b403bf06b7/geolookup/q/autoip.json")
.then(res => dispatch(getCurrentConditions(res.data.location.l)))
.catch(err => dispatch({type: CURRENT_CONDITION_ERROR, error: err}));
}
}
export function getCurrentConditions(location) {
return (dispatch, getState) => {
fetch(`http://api.wunderground.com/api/777ba6b403bf06b7/conditions/${location}.json`)
.then(condition => dispatch({type: CURRENT_CONDITION_SUCCESS, condition: condition.data.current_observation, input: ''}))
.then(() => {
if(getState.isLoading) {
setTimeout(() => {
dispatch({type: LOAD, bool: false})
}, 2000)
}
})
.catch(err => dispatch({type: CURRENT_CONDITION_ERROR, error: err.data}));
}
}
知道为什么吗?
检查网络选项卡,我知道我收到了200的响应,其中包含我期望的所有数据,但是没有调用dispatch(getCurrentConditions)
但是,如果我用axios.get替换fetch,一切都会正常。在fetch()的情况下,devtools控制台不会记录不同的消息?根本没有。initializeLoad中的fetch调用正在被调用,但它从未分派getCurrentConditions。在fetch()情况下,没有其他消息记录到devtools控制台?没有。initializeLoad中的fetch调用正在被调用,但它从不分派getCurrentConditions。