简体   繁体   English

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

[英]Error: Actions must be plain objects. Use custom middleware for async actions. How to fix it?

I've a problem with the redux action.我对 redux 操作有疑问。 When I submit a form the following error shows me.当我提交表单时,会显示以下错误。 Network shows that data has been transferred.网络显示数据已传输。 how to fix it?如何解决? I don't know where the error may be anymore我不知道错误可能在哪里了

My Error我的错误

Error: Actions must be plain objects. Use custom middleware for async actions.

  19 | const handleSumbitAddExpenses = (values) => {
  20 |   console.log(allCategories);
  21 |   allCategories.forEach(category => {
> 22 |     addExpenses({
     | ^  23 |       categoryId: category.categoryId,
  24 |       data: values,
  25 |     })



  18 | 
  19 |      const handleSumbitAddExpenses = (values) => {
  20 |        console.log(allCategories);
> 21 |        allCategories.forEach(category => {
     | ^  22 |          addExpenses({
  23 |            categoryId: category.categoryId,
  24 |            data: values,```

Redux Action Redux 动作

export const addExpenses = ({categoryId, data}) => async(dispatch) => {
    dispatch({
        type: ADDEXPENSES_GET_REQUEST,
    })

    try {
        const response = await API.expenses.addExpenses({
            categoryId,
            data
        });
        const data = await response.json();
        dispatch({
            type: ADDEXPENSES_GET_SUCCES,
            payload: data,
        })
    }catch (error) {
        dispatch({
            type: ADDEXPENSES_GET_FAILURE,
        })
    }

}

Function in component Function 在组件

      const handleSumbitAddExpenses = (values) => {
        console.log(allCategories);
        allCategories.forEach(category => {
          addExpenses({
            categoryId: category.categoryId,
            data: values,
          })
        })
        
      }

This's my Store setup its.这是我的商店设置。 Simple setup from redux thunk documentation. redux thunk 文档中的简单设置。

import { applyMiddleware, createStore } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import thunkMiddleware from "redux-thunk";

import rootReducer from "./reducers";

export default function configureStore(preloadedState) {
  const middlewares = [thunkMiddleware];
  const middlewareEnhancer = applyMiddleware(...middlewares);

  const enhancers = [middlewareEnhancer];
  const composedEnhancers = composeWithDevTools(...enhancers);

  const store = createStore(rootReducer, preloadedState, composedEnhancers);

  if (process.env.NODE_ENV !== "production" && module.hot) {
    module.hot.accept("./reducers", () => store.replaceReducer(rootReducer));
  }

  return store;
}

and my fetch和我的获取

export const addExpenses = ({ categoryId, data }) => {
  const promise = fetch(
    `
    ${process.env.REACT_APP_API_URL}/categories/${categoryId}/transactions`,
    {
      method: "POST",
      headers: {
        "Content-type": "application/json",
      },
      body: JSON.stringify(data),
    }
  );

  return promise;
};

at first redux actions should return plain objects only.首先 redux 动作应该只返回普通对象。 However you are returning another function that takes dispatch as parameter to run some async code.但是,您将返回另一个 function ,它将调度作为参数来运行一些异步代码。

this action implementation is typical from redux-thunk middleware.这个动作实现是redux-thunk中间件的典型实现。 redux-thunk intercepts the dispatch call if you return another function, enabling you to make async requests.如果您返回另一个 function,redux-thunk 会拦截调度调用,使您能够发出异步请求。 But first you need to install redux-thunk npm install redux-thunk , then apply it as middleware at your store creation like:但首先您需要安装 redux-thunk npm install redux-thunk ,然后在您的商店创建中将其作为中间件应用:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';

const store = createStore(rootReducer, applyMiddleware(thunk));

once your middleware is properly implemented your actions that make async calls should work as expected.一旦您的中间件被正确实施,您进行异步调用的操作应该按预期工作。

暂无
暂无

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

相关问题 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.? 如何解决“动作必须是普通对象。 使用自定义中间件执行异步操作。” - How to fix “Actions must be plain objects. Use custom middleware for async actions.” 如何解决“操作必须是普通对象。 使用自定义中间件执行异步操作。” - How to fix '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? 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error Redux 给出错误“错误:操作必须是普通对象。 使用自定义中间件进行异步操作。” - Redux gives error “Error: Actions must be plain objects. Use custom middleware for async actions.” 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 - 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 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 待办事项应用 - Error: Actions must be plain objects. Use custom middleware for async actions. ToDo app 为什么我会收到错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 - Why to do I get error: Actions must be plain objects. Use custom middleware for async actions.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM