[英]Typescript, React and Redux axios Error - actions must be plain objects. use custom middleware for async actions
Need some Help With Typescript, React and Redux.在 Typescript、React 和 Redux 方面需要一些帮助。
export function fetchAllMeals (subject: string){
axios
.get(`https://www.themealdb.com/api/json/v1/1/search.php?s=${subject}`)
.then((response: any) => {
console.log(response.data)
return { type: FETCH_ALL_MEALS, data: response.data };
}
)
.catch(err => console.log(err));
};
I keep getting this error is actions must be plain objects. use custom middleware for async actions
我不断收到此错误,因为
actions must be plain objects. use custom middleware for async actions
actions must be plain objects. use custom middleware for async actions
Or when I do:或者当我这样做时:
export function fetchAllMeals (subject: string): MealActionTypes {
axios
.get(`https://www.themealdb.com/api/json/v1/1/search.php?s=${subject}`)
.then((response: any) => {
console.log(response.data)
return { type: FETCH_ALL_MEALS, data: response.data };
}
)
.catch(err => console.log(err));
};
Or this error is A function whose declared type is neither 'void' nor 'any' must return a value.
或者这个错误是
A function whose declared type is neither 'void' nor 'any' must return a value.
- I have changed : MealActionTypes
in top line - 我已更改
: MealActionTypes
In redux, all the actions should be synchronous.在 redux 中,所有动作应该是同步的。 However in your case you are doing an http call with
axios
and so it is an asynchronous operation.但是,在您的情况下,您正在使用 axios 进行
axios
调用,因此这是一个异步操作。
The error message that you get actions must be plain objects. use custom middleware for async actions
您获得
actions must be plain objects. use custom middleware for async actions
actions must be plain objects. use custom middleware for async actions
is then correct. actions must be plain objects. use custom middleware for async actions
是正确的。
So if you want to do async actions, you need to use a async middleware.所以如果你想做异步操作,你需要使用异步中间件。 The easiest one is thunk
最简单的是thunk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.