【问题标题】:Dispatching first runs undefined and then resolves in Redux Thunk调度首先运行 undefined,然后在 Redux Thunk 中解析
【发布时间】: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())
  });

Thunk dispatching with the actual text first

【问题讨论】:

    标签: reactjs redux react-redux redux-thunk


    【解决方案1】:
    useEffect(() => {
      dispatch(fetchCountryInfo())
    }, []);
    

    添加[] 不带任何部门,它只会调度一次

    【讨论】:

    • 我试过了,但这是一个不同的问题,请参阅链接。第一次分派时,它会将函数本身作为操作返回。
    猜你喜欢
    • 2021-07-12
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2021-12-18
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多