我创建了一个React App,我需要通过Redux发出异步请求, 我想使用redux-thunk来获取但出现此错误。
未捕获的错误:动作必须是纯对象。使用自定义中间件执行异步操作
这是我的代码:
export function firewall(state = { data: [], loading: false, error: false }, action) {
switch (action.type) {
case 'REQUESTED_DATA_FAILED':
return {
data: [],
loading: false,
error: true,
};
case 'REQUESTED_DATA_SUCCEEDED':
return {
data: action.data,
loading: false,
error: false,
};
case 'REQUESTED_DATA':
return {
data: [],
loading: true,
error: false,
};
default:
return state;
}
}
const requestData = () => {
return { type: 'REQUESTED_DATA' }
};
const requestDataSuccess = (data) => {
return { type: 'REQUESTED_DATA_SUCCEEDED', data: data }
};
const requestDataError = () => {
return { type: 'REQUESTED_DATA_FAILED' }
};
export const getFirewall = () => {
return dispatch => {
dispatch(requestData())
request('/list', 'get')
.then(res =>
dispatch(requestDataSuccess(formatData(res.data))))
.catch(error => dispatch(requestDataError()))
}
}
这是我的商店
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk';
import { Reducers } from './reducer'
export const store = createStore(
Reducers,
applyMiddleware(thunk)
);