简体   繁体   English

Typescript、React 和 Redux axios 错误 - 操作必须是普通对象。 使用自定义中间件进行异步操作

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

相关问题 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error React-Redux:动作必须是普通对象。 使用自定义中间件进行异步操作错误 - React-Redux: Actions must be plain objects. Use custom middleware for async actions Error react-redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - react-redux Error: Actions must be plain objects. Use custom middleware for async actions React Native 和 Redux - 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - React Native & Redux - Error: Actions must be plain objects. Use custom middleware for async actions React / Redux:错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - React/Redux: Error: Actions must be plain objects. Use custom middleware for async actions 异步操作 Redux 未处理拒绝(错误):操作必须是普通对象。 使用自定义中间件进行异步操作 - Async Action Redux Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions React-Actions 必须是普通对象。 在 Redux 中使用自定义中间件进行异步操作 - React- Actions must be plain objects. Use custom middleware for async actions in Redux React-Redux-Saga:动作必须是普通对象。 使用自定义中间件进行异步操作 - React-Redux-Saga: Actions must be plain objects. Use custom middleware for async actions 动作必须是普通对象。 使用自定义中间件进行异步操作。 React,Redux,Thunk - Actions must be plain objects. Use custom middleware for async actions. React, Redux, Thunk React redux 动作必须是普通对象。 使用自定义中间件进行异步操作 - React redux Actions must be plain objects. Use custom middleware for async actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM