简体   繁体   English

react-redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作

[英]react-redux Error: Actions must be plain objects. Use custom middleware for async actions

I have a very simple code for incrementing and decrementing when + or - buttons are pressed.我有一个非常简单的代码,用于在按下 + 或 - 按钮时递增和递减。 but whenever I press the buttons, I receive the error.但每当我按下按钮时,我都会收到错误消息。 I've been working on it for about 4 hours and can't find out why.我已经研究了大约 4 个小时,但找不到原因。 I'm pretty sure that I'm not doing any async actions and my actions are returning JavaScript objects我很确定我没有执行任何异步操作并且我的操作正在返回 JavaScript 对象

actions code:动作代码:

export const increment = () => {
    return { type: 'INCREMENT' }
}

export const decrement = () => {
    return { type: 'DECREMENT' }
}

reducer code:减速机代码:

export default function counterReducer(state = 0, action) {
    switch (action.type) {
      case 'INCREMENT':
        return state + 1
      case 'DECREMENT':
        return state - 1
      default:
        return state
    }
  }

app component code:应用组件代码:

import React from "react";
import {decrement, increment } from "./actions";
import { useSelector, useDispatch } from "react-redux";
export default function App() {
  const counter = useSelector((state) => state.counter);
  const dispatch = useDispatch();
  return (
    <div>
      <button onClick={() => dispatch(increment)}>+</button>
      <button onClick={() => dispatch(decrement)}>-</button>
      <p>{counter}</p>
    </div>
  );
}

Much appreciated.非常感激。

In Redux, actions are objects .在 Redux 中, 动作是对象 You have to execute actions functions into dispatch: dispatch(increment) could be dispatch(increment())你必须在 dispatch(increment) 中执行 action 函数: dispatch(increment)可以是dispatch(increment())

You are dispatching action creator functions not the actions您正在调度动作创建者功能而不是动作

The increment and decrement functions are called action creators.递增和递减函数称为动作创建者。 They return the action object.他们返回操作 object。

You should call the action creator function for dispatching an action.您应该调用动作创建者 function 来调度动作。

<button onClick={() => dispatch(**increment()**)}>+</button>

That problem is solved by using a middleware like redux-thunk or saga in the store configuration.这个问题可以通过在 store 配置中使用像redux-thunksaga这样的中间件来解决。

import { createStore, applyMiddleware } from "redux";

import rootReducer from "../reducers";
import thunk from "redux-thunk";

//Applying redux-thunk solves the problem
//Actions must be plain objects. Use custom middleware for async actions.

export const store = createStore(rootReducer, applyMiddleware(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 Native 和 Redux - 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - React Native & Redux - Error: Actions must be plain objects. Use custom middleware for async actions Typescript、React 和 Redux axios 错误 - 操作必须是普通对象。 使用自定义中间件进行异步操作 - Typescript, React and Redux axios 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