简体   繁体   English

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

[英]Redux gives error “Error: Actions must be plain objects. Use custom middleware for async actions.”

I'm very new to redux.我对 redux 很陌生。 I tried my best to resolve this error in redux but it still gives me an error.我尽力解决 redux 中的这个错误,但它仍然给我一个错误。 The error is message is:错误是消息是:

Error: Actions must be plain objects.错误:操作必须是普通对象。 Use custom middleware for async actions.使用自定义中间件进行异步操作。

Please help me resolve this.请帮我解决这个问题。

The error points to the line after return.错误指向返回后的行。 Here is my code:这是我的代码:

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

And fetchState is defined in my action.jsx并且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);
            });
    }
}

You have to use async and await:您必须使用异步和等待:

export const fetchState = async (e) =>{

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error Redux返回错误'操作必须是普通对象。 使用自定义中间件进行异步操作。 即使中间件已定义 - Redux returns error 'Actions must be plain objects. Use custom middleware for async actions.' even though middleware is defined Error get'Uncaught Error:操作必须是纯对象。 使用自定义中间件执行异步操作。” 由redux-thunk - Error get 'Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.' by redux-thunk 未捕获的错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 即使我在redux中使用compose - Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. even if I am using compose in redux 为什么我会收到错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 - Why to do I get error: Actions must be plain objects. Use custom middleware for async actions.? 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 如何解决? - Error: Actions must be plain objects. Use custom middleware for async actions. How to fix it? 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.? 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 如何解决? - Error: Actions must be plain objects. Use custom middleware for async actions. How to solve it? 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 - Error: Actions must be plain objects. Use custom middleware for async actions. 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 在 React Native 中 - Error: Actions must be plain objects. Use custom middleware for async actions. in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM