[英]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.