redux-thunk get错误:未被捕获的错误:动作必须是普通对象

时间:2018-10-14 10:29:59

标签: react-redux redux-thunk

我创建了一个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)
);

0 个答案:

没有答案