【问题标题】:"Actions must be plain objects. Use custom middleware for async actions." with react/redux“操作必须是普通对象。使用自定义中间件进行异步操作。”与反应/减少
【发布时间】:2017-09-05 18:55:33
【问题描述】:

我遇到了这个错误,并花了几个小时试图解决这个问题。我查看了所有看似重复的问题 - 但它们并没有解决问题。

在我的 react/redux 应用程序中,当我在其中一个操作中发出 ajax 请求时,它会抛出此错误: Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.

我的商店创建如下所示:

import { composeWithDevTools } from 'redux-devtools-extension';
import reducers from './reducers';
import thunkMiddleware from 'redux-thunk';
import { applyMiddleware, createStore } from 'redux';

export default createStore(reducers, composeWithDevTools(
	applyMiddleware(thunkMiddleware)
));

相关的reducer长这样:

import * as actions from './../../actions/tools/vehicle-lookup';

const defaultState = {
	vrm: void 0,
	isLoading: false,
	response: void 0,
	error: void 0,
};

export default function (state = defaultState, action) {
	switch (action.type) {
		case actions.VEHICLE_LOOKUP:
			return { ...state, isLoading: true, vrm: action.vrm };

		case actions.VEHICLE_LOOKUP_SUCCESS:
			return { ...state, isLoading: false, payload: action.payload, error: void 0 };

		case actions.VEHICLE_LOOKUP_FAILURE:
			return { ...state, isLoading: false, error: action.error, response: void 0 };

		default: return state;
	}
}

相关操作如下所示:

import axios from 'axios';

export const VEHICLE_LOOKUP = 'VEHICLE_LOOKUP';
export const VEHICLE_LOOKUP_SUCCESS = 'VEHICLE_LOOKUP_SUCCESS';
export const VEHICLE_LOOKUP_FAILURE = 'VEHICLE_LOOKUP_FAILURE';

export function fetchVehicleLookup(vrm: string, jwt: string) {
	return function (dispatch) {
		dispatch(requestVehicleLookup());

		axios.create({
			timeout: 4000,
		})
			.post('/*api url*', {
				action: '*method*',
				body: { vrm },
			})
			.then(response => response.data)
			.then(json => dispatch(receiveVehicleData(json)))
			.catch(error => dispatch(receiveVehicleDataFailure(error)));
	};
}

function requestVehicleLookup() {
	return { type: VEHICLE_LOOKUP };
}

function receiveVehicleData(payload: object) {
	return { type: VEHICLE_LOOKUP_SUCCESS, payload };
}

function receiveVehicleDataFailure(error: object) {
	return { type: VEHICLE_LOOKUP_FAILURE, error };
}

我的包版本是:

"axios": "^0.16.0",
"react": "^15.4.2",
"react-addons-css-transition-group": "^15.5.0",
"react-addons-transition-group": "^15.5.0",
"react-dom": "^15.4.2",
"react-hot-loader": "^3.0.0-beta.6",
"react-redux": "^5.0.3",
"react-router": "^4.0.0",
"react-router-dom": "^4.0.0",
"redux": "^3.6.0",
"redux-devtools-extension": "^2.13.0",
"redux-promise": "^0.5.3",
"redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.2.0",

【问题讨论】:

  • 您能否还包括发送异步操作的代码?

标签: reactjs redux react-redux axios redux-thunk


【解决方案1】:

首先想到的是您的 fetchVehicleLookup 操作在抱怨,因为您返回的是 axios 而不是仅仅在内部调度。

export function fetchVehicleLookup(vrm: string, jwt: string) {
    return function (dispatch) {
        dispatch(requestVehicleLookup());

        axios.create({
            timeout: 4000,
        })
            .post('/*api url*', {
                action: '*method*',
                body: { vrm },
            })
            .then(response => response.data)
            .then(json => dispatch(receiveVehicleData(json)))
            .catch(error => dispatch(receiveVehicleDataFailure(error)));
    };
}

只需删除您操作中的 return 语句,因为它返回 axios 表示的任何对象,我想这将是某种形式的 Promise

第二个想法可能是围绕您的商店设置,因为听起来thunk 实际上不起作用。

【讨论】:

  • 我已经从 axios 中删除了该返回,但遗憾的是仍然存在相同的错误 - 将编辑以从我的操作中删除
  • 为了测试,删除整个axios 部分,然后只发送一个虚拟操作。错误仍然存​​在吗?
【解决方案2】:

我设法弄明白了——在与@Glitch100 聊了很长时间之后(谢谢!)。在创建 axios 承诺之后,我需要返回 requestVehicleLookup 的调度。像这样:

export function fetchVehicleLookup(vrm: string, jwt: string) {
	return function (dispatch) {
		axios.create({
			timeout: 4000,
		})
			.post('/*api url*', {
				action: '*method*',
				body: { vrm },
			})
			.then(response => response.data)
			.then(json => dispatch(receiveVehicleData(json)))
			.catch(error => dispatch(receiveVehicleDataFailure(error)));
      
      
		return dispatch(requestVehicleLookup());
	};
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 2020-06-17
    相关资源
    最近更新 更多