Redux操作类型为PROBE_UNKNOWN_ACTION

时间:2017-09-21 14:38:35

标签: redux react-redux

我正在调用操作requestLoadOrders来获取我需要的订单。我发送的是REQUEST类型,之后是SUCCESSFAILURE。获取成功是因为我的订单位于redux dev-tools的有效负载中,但我在reducer中收到的操作是@@redux/PROBE_UNKNOWN_ACTION_z.r.p.l.z。我找到了关于这个here的帖子,但我似乎无法找到我做错的事情?

redux dev-tools

actions.js

import {
  LOAD_ORDERS_REQUEST,
  LOAD_ORDERS_SUCCESS,
  LOAD_ORDERS_FAILURE
} from './constants';

import { fetchOrders } from '../../api';

export const requestLoadOrders = () => {
  return (dispatch, getState) => {
    dispatch({ type: LOAD_ORDERS_REQUEST });
    fetchOrders().then(orders => {
      dispatch({ type: LOAD_ORDERS_SUCCESS, payload: orders});
    }).catch(error => {
      console.error(error);
      dispatch({ type: LOAD_ORDERS_FAILURE, payload: error});
    });
  };
};

reducer.js

import {
  LOAD_ORDERS_REQUEST,
  LOAD_ORDERS_SUCCESS,
  LOAD_ORDERS_FAILURE
} from './constants';

const initialState = {
  orders: []
};

const orderReducer = ( state = initialState, { payload, type }) => {
  switch (type) {
    case LOAD_ORDERS_REQUEST :
      return state;
    case LOAD_ORDERS_SUCCESS :
      return { ...state, orders: payload};
    case LOAD_ORDERS_FAILURE :
      return { ...state, error: payload.error};
    default :
      return state;
  }
};

export default orderReducer;

我的操作得到正确发送,但我认为reducer接收数据时出现问题。因此,我还添加了我的商店和合并缩减器文件。

store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';
import thunk from 'redux-thunk';
import makeRootReducer from './reducers';

export const history = createHistory();

const initialState = {}
const enhancers = [];
const middleware = [ routerMiddleware(history), thunk ];

if (process.env.NODE_ENV === 'development') {
  const devToolsExtension = window.devToolsExtension;

  if (typeof devToolsExtension === 'function') {
    enhancers.push(devToolsExtension());
  }
}

const composedEnhancers = compose(
  applyMiddleware(...middleware),
  ...enhancers
);

const store = createStore(
  makeRootReducer,
  initialState,
  composedEnhancers
);

export default store;

reducers.js

import { combineReducers } from 'redux';
import orderReducer from '../modules/Order/reducer';

export const makeRootReducer = asyncReducers => {
  return combineReducers({
    order: orderReducer,
    ...asyncReducers
  });
}

export default makeRootReducer;

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误。我应该通过在createStore()中的单词后添加括号来执行makeRootReducer函数。

store.js 的createStore()部分更新为:

const store = createStore(
  makeRootReducer(),
  initialState,
  composedEnhancers
);

是修复。