2

所以我有这个简单的异步请求动作创建器,它使用axios请求请求和redux-thunk调度动作。我想为不同的请求重新使用相同的动作创建者——所以每个请求都有自己的状态。我会尝试展示一些代码,然后再解释更多。这是我的动作创建者

function responseOK(response){
  return {
    type: RESPONSE_OK,
    payload: response,
  }
}

function loadingChangedStatus(isLoading) {
  return {
    type: IS_LOADING,
    isLoading:  isLoading,
  }
}

function handleError(errorID){
  return {
    type: HANDLE_ERROR,
    errorID: errorID,
  }
}



export function makeRequest( URL){

  return function(dispatch, getSate){

      dispatch( loadingChangedStatus(true) );

      axios.get( URL )
        .then(function(response){
          dispatch(loadingChangedStatus(false));
          dispatch(responseOK(response));

        })
        .catch(function (response) {
          dispatch(loadingChangedStatus(false));
          dispatch(handleError('connection_error'));
        });
  }

}

还有我的减速器reducerRequest

export default function(state = {isLoading:false, payload:false,errorID:false}, action){

  switch (action.type) {

    case IS_LOADING:
      return Object.assign({}, state, {
        isLoading: action.isLoading,
      });
      break;

    case RESPONSE_OK:
        return Object.assign({}, state, {
          isLoading: state.isLoading,
          payload: action.payload,
        });
        break;

    case HANDLE_ERROR:
        return Object.assign({}, state, {
          isLoading: state.isLoading,
          payload: action.payload,
          errorID:action.errorID,
        });
        break;

    default:
      return state;
  }

}

这里开始我的问题

我像这样组合减速器:

import { combineReducers } from 'redux';
import Request from "./reducerRequest";

const rootReducer = combineReducers({
  // I WANT EACH OF THESE TO BE SEPARATE INSTANCE BUT USE THE SAME ACTION CREATOR / REDUCER
  defaults: Request,
  updates: Request,
});

export default rootReducer;

在我的组件中:

function mapStateToProps( {defaults, updates} ){
  return {defaults, updates} 
}

function mapDispatchToProps( dispatch ){
  return bindActionCreators({ makeRequest}, dispatch);
} 

问题:我想为不同的请求重新使用我的动作创建器。我怎样才能

  1. 打电话makeRequest('www.defaults.com'),它最终在defaults
  2. 打电话makeRequest('www.updates.com'),它最终在updates

现在我可以想象解决这个问题的唯一方法是为每个请求编写它自己的动作创建者和自己的减速器 - 只是大量的复制粘贴 - 这感觉不对。

我怎样才能重用我的动作创建器和reducer来在我的组件中创建2个单独的defaults实例updates

4

1 回答 1

1

您可以为每个操作添加减速器操作的前缀:

export default function(namespace) 
   return function(state = {isLoading:false, payload:false,errorID:false}, action){

  switch (action.type) {

    case namespace + IS_LOADING:
      return Object.assign({}, state, {
        isLoading: action.isLoading,
      });
      break;

    case namespace + RESPONSE_OK:
        return Object.assign({}, state, {
          isLoading: state.isLoading,
          payload: action.payload,
        });
        break;

    case namespace + HANDLE_ERROR:
        return Object.assign({}, state, {
          isLoading: state.isLoading,
          payload: action.payload,
          errorID:action.errorID,
        });
        break;

    default:
      return state;
  }
}
}

然后添加命名空间

function responseOK(namespace, response){
  return {
    type: namespace + RESPONSE_OK,
    payload: response,
  }
}


const rootReducer = combineReducers({
  // I WANT EACH OF THESE TO BE SEPARATE INSTANCE BUT USE THE SAME ACTION CREATOR / REDUCER
  defaults: Request("DEFAULTS_"),
  updates: Request("UPDATES_"),
});

然后在调用 make 请求时使用命名空间

call makeRequest('DEFAULTS_', 'www.defaults.com') and it ends up in defaults
call makeRequest('UPDATES_', 'www.updates.com') and it ends up in updates

高温高压

于 2016-05-25T09:56:11.063 回答