简体   繁体   English

无法在 react-redux 中的操作中调用另一个操作

[英]Not able to call another action inside a action in react-redux

I am trying to load the data from web api call , for this i have added two action one for calling the webapi method another for loading the data.我正在尝试从 web api call 加载数据,为此我添加了两个操作,一个用于调用 webapi 方法,另一个用于加载数据。 My actions are like this:-我的行为是这样的:-

export function LoadLabResult (labresult) {

  return (dispatch) => {
    dispatch({
      type: 'RECEIVE_LABRESULT',
    });

    fetch('http://localhost:8090/api/Requisition/GetRequisitionTestInfo', {mode: 'no-cors'})
  .then(response =>dispatch({
        type: 'REQUEST_LABRESULT',
        labresult:response.data
      }));

  }
};
export function receiveLabResult(labresult) {
console.log(labresult);
  return {
    type:'REQUEST_LABRESULT',
    labresult:labresult
  };
}

Now the issue is that it is not calling the receiveLabResult method.How can I do this?现在的问题是它没有调用receiveLabResult 方法。我该怎么做? How can I pass the data to labresult ?如何将数据传递给labresult

Thank you guys so much after 3 hours I finally solved my problem.非常感谢你们,3小时后我终于解决了我的问题。 Actually if you want to call another action function in your function you must call it inside the dispatch() function as a parameter .实际上,如果你想在你的函数中调用另一个动作函数,你必须在dispatch()函数中调用它作为参数 example: dispatch(loadUser());示例: dispatch(loadUser());

case1: if you trying to call the action creator from another action creator, yo can call that action directly as a function call.案例1:如果您尝试从另一个动作创建者调用动作创建者,您可以直接将该动作作为函数调用来调用。

export function LoadLabResult (labresult) {

  return (dispatch) => {
    dispatch(receiveLabResult());
  }
};
export function receiveLabResult(labresult) {
console.log(labresult);
  return {
    type:'REQUEST_LABRESULT',
    labresult:labresult
  };
}

case2: if you trying to call action creator from component use dispatch which will be injected from redux using connect() function.案例2:如果您尝试从组件调用动作创建者,请使用 dispatch ,它将使用 connect() 函数从 redux 注入。

Edited to configure redux thunk middleware编辑以配置 redux thunk 中间件

//Middleware
const middleware = [
  thunk,
  promiseMiddleware()
];

// Apply all the middleware for Redux
const enhancers = composeEnhancers(
  applyMiddleware(...middleware)
);

// Create the Redux Store
const store = createStore(rootReducer, initialState, enhancers);

Finally I got the solution:-最后我得到了解决方案:-

My first Action will be Like this:-我的第一个动作将是这样的:-

export  function LoadAllLabResult (selectedUserId) {
    return (dispatch) => {
    dispatch({
      type: REQUEST_LABRESULT,//defining the name of the action to identify in the reducer
    });

  fetch(APIPATH+'data/LoadTestInfo?patientVisitId='+selectedUserId)//calling the service
  .then((response) => response.json())
    .then((labresult) => dispatch(receiveLabResult(labresult)));//passing the data to other actions
  }
}

My second action will be like this:-我的第二个动作是这样的:-

 export  function receiveLabResult(labresult) {
      return {
        type:RECEIVE_LABRESULT,//defining the name of the action to identify in the reducer
        labresult:labresult//passing the data to reducer
      };
    }

Now from Reducer we will call the actions as:-现在从 Reducer 中,我们将调用操作为:-

import {
  RECEIVE_LABRESULT,
  REQUEST_REQUISITION
} from '../../../Constant/actionType'
//define the initail state for the reducer
var initialState={
     labresult: [],
  loadinglabResult: true
}
//call the action to update the sate
 function labResultReducer(state =initialState, action) {
    switch (action.type) {
  case RECEIVE_LABRESULT:
        return Object.assign({}, state, {
          labresult: action.labresult,
          loadinglabResult: false,
        });
    case REQUEST_REQUISITION:
        return Object.assign({}, state, {
          loadinglabResult: true

        });

    default:
        return state;
    }
}
export default labResultReducer;

The correct way is that you have to call the method receiveLabResult directly after the API success handler by passing that response.data.正确的方法是您必须通过传递 response.data 直接在 API 成功处理程序之后调用方法receiveLabResult

You return an Action Object with type when you want to go to the Reducer or notify the reducer about the dispatch.当您想要转到 Reducer 或通知 reducer 调度时,您返回一个带有类型的 Action 对象。 So you have to write the following code to make it work:因此,您必须编写以下代码才能使其工作:

 export function LoadLabResult (labresult) { return (dispatch) => { dispatch({ type: 'RECEIVE_LABRESULT', }); fetch('http://localhost:8090/api/Requisition/GetRequisitionTestInfo', {mode: 'no-cors'}) .then(response =>dispatch(receiveLabResult(response.data)); } }; export function receiveLabResult(labresult) { console.log(labresult); return { type:'REQUEST_LABRESULT', labresult:labresult }; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM