【发布时间】:2022-01-04 15:19:12
【问题描述】:
我正在尝试使用 Redux thunk 从 API 获取数据并进入 redux,但我遇到了一个问题,即 redux 分派两次,一次使用未定义的操作,一次使用实际操作。我尝试改变我的动作,改变我的减速器和我的 thunk 但似乎没有任何效果。我之前已经成功使用过 redux thunk 并且从未遇到过这个问题。
我的thunk函数
export const fetchCountryInfo = () => {
return async (dispatch,getState) => {
const response = await Axios.get(https://api.covid19api.com/summary)
console.log(response.data)
dispatch({
type:'updateCountryData',
payload:response.data.Countries
})
}
}
我的减速器:`const reducer = (state = initialState, action) => {
返回 action.payload }`
调度动作
const dispatch = useDispatch();
useEffect(() => {
dispatch(fetchCountryInfo())
});
【问题讨论】:
标签: reactjs redux react-redux redux-thunk