Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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? How can I pass the data to labresult?

like image 814
Gorakh Nath Avatar asked Jan 03 '17 05:01

Gorakh Nath


People also ask

Does Redux support multiple actions?

In Redux, actions are the JavaScript object which has information. Having multiple actions will need multiple action creators.

Can Redux actions dispatch other actions?

However, Redux's middleware makes it possible to intercept dispatched actions and add additional complex behavior around them, including side effects.

How do you call an action in react Redux?

You can dispatch an action by directly using store. dispatch(). However, it is more likely that you access it with react-Redux helper method called connect(). You can also use bindActionCreators() method to bind many action creators with dispatch function.

What happens when you try to dispatch an action within a reducer?

Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.


1 Answers

Thank you guys so much after 3 hours I finally solved my problem. Actually if you want to call another action function in your function you must call it inside the dispatch() function as a parameter. example: dispatch(loadUser());

like image 64
AdibTE Avatar answered Sep 18 '22 00:09

AdibTE