Redux 给出错误“错误:操作必须是普通对象对异步操作使用自定义中间件”

问题描述

我对 redux 很陌生。我尽力在 redux解决这个错误,但它仍然给我一个错误错误消息是:

错误:操作必须是普通对象。使用自定义中间件进行异步 动作。

请帮我解决这个问题。

错误指向返回后的行。这是我的代码

    const mapdispatchToProps = (dispatch) => {
      return{
        fetchState: (e) => dispatch(fetchState(e)),fetchCapital: (e) => dispatch(fetchCapital(e))
      }
    }

并且 fetchState 在我的 action.jsx 中定义

export const fetchState = (e) =>{
    console.log(e.target)
    return (dispatch) => {
        console.log("hey")

        axios.get('http://localhost:8080/StatesByCountry/'+ e.target.value)
                .then(response => {
                    const stateData = response.data || '';
                    dispatch(changeState(stateData));
            })
            .catch( err => {
                console.log(err);
            });
    }
}

解决方法

你必须使用异步和等待:

export const fetchState = async (e) =>{

await axios.get('http://localhost:8080/StatesByCountry/'+ e.target.value)