redux-saga icon indicating copy to clipboard operation
redux-saga copied to clipboard

Why API is getting called twice in redux-saga?

Open iamrahman opened this issue 5 years ago • 17 comments

So, I am trying to dispatch an action to get a request. But I can see that the API is getting called twice but the action dispatched once. Please Help in this -

I did console.log() in both function forgetPasswordSaga and onForgetPasswordButtonClick Result : forgetPasswordSaga is called twice and onForgetPasswordButtonClick called once Component

const Login = (props: Props) => {
 const { dispatch } = props;

 const onForgetPasswordButtonClick = (data: ForgetPasswordData) => {
         dispatch({
           type: 'LOGINPAGE_FORGET_PASSWORD_REQUEST',
           payload: { data },
         });
      };
 
 const mapStateToProps = (state: any) => {
   const { loading } = state.ui;
   return {
     loading
   };
 };

 export default connect(mapStateToProps, null)(Login);

Saga

const forgetPasswordSaga = function* (action: Action) {
      try {
        const response = yield call(forgetPassword, action.payload);
        yield put({ type: FORGET_PASSWORD_SUCCESS });
        Router.go(RoutePath.LOGIN);
        return response;
      } catch (e) {
        yield put({ type: FORGET_PASSWORD_FAIL });
        yield put({ type: ERROR_SET, metadata: { message: Error.getErrorMessage(e) } });
      }
    };
    
    export default [
      takeLatest('LOGINPAGE_FORGET_PASSWORD_REQUEST', forgetPasswordSaga),
    ];

iamrahman avatar Jul 29 '20 11:07 iamrahman

@iamrahman This has been happening to me as well with a project for the past 7-8 months. If you did / do find a solution , please post it here as well

Roeefl avatar Aug 23 '20 01:08 Roeefl

@Roeefl There could be many possibilities from which this problem could occur. In my case, I was working with multiple sagas. So there is a function called rootSaga where all the other sagas are mentioned.

I was calling the authSaga twice

export default function* rootSaga() {
  yield all([
    ...assetSaga,      
    ...authSaga,       // here
    ...clientSaga,
    ...fileSaga,
    ...formatSaga,
    ...messagingSaga,
    ...notificationSaga,
    ...projectSaga,
    ...userSaga,
    ...authSaga,        // and here
  ]);

iamrahman avatar Aug 23 '20 13:08 iamrahman

same here

BuistvoPloti avatar Dec 22 '20 12:12 BuistvoPloti

Thanks @iamrahman i was calling twice in rootSaga removing one solved my bug

rehanmohiuddin avatar Mar 22 '21 11:03 rehanmohiuddin

Same here, so now any one help me fix it ! :( 1 2 3 4

tuanhai2712 avatar Aug 10 '21 11:08 tuanhai2712

I'm also having this issue

UPDATE: solved. I think I also had duplicated root saga call

handhikadj avatar Oct 04 '21 10:10 handhikadj

Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. OSS maintainers usually can't afford the time to set up the repro, even if exact steps are given.

Andarist avatar Oct 04 '21 14:10 Andarist

Closing due to inactivity. Please feel free to reply if you are still having this issue and I’d be happy to help debug.

neurosnap avatar Feb 27 '22 03:02 neurosnap

@Roeefl There could be many possibilities from which this problem could occur. In my case, I was working with multiple sagas. So there is a function called rootSaga where all the other sagas are mentioned.

I was calling the authSaga twice

export default function* rootSaga() {
  yield all([
    ...assetSaga,      
    ...authSaga,       // here
    ...clientSaga,
    ...fileSaga,
    ...formatSaga,
    ...messagingSaga,
    ...notificationSaga,
    ...projectSaga,
    ...userSaga,
    ...authSaga,        // and here
  ]);

thanks alot man same problem i was doing i spent approx 3 hours.................

mastercodebegin avatar Mar 19 '22 11:03 mastercodebegin

@Roeefl There could be many possibilities from which this problem could occur. In my case, I was working with multiple sagas. So there is a function called rootSaga where all the other sagas are mentioned.

I was calling the authSaga twice

export default function* rootSaga() {
  yield all([
    ...assetSaga,      
    ...authSaga,       // here
    ...clientSaga,
    ...fileSaga,
    ...formatSaga,
    ...messagingSaga,
    ...notificationSaga,
    ...projectSaga,
    ...userSaga,
    ...authSaga,        // and here
  ]);

You Saved my day. Thank you,.

KarthikBaleneni avatar Jun 28 '22 09:06 KarthikBaleneni

@kartiikeyabaleneni I wish I could say the same 😭 but I'm glad to hear that you have managed to resolve the issue

Roeefl avatar Jun 29 '22 14:06 Roeefl

Hi @Roeefl

How can I help? If you share some code I'll be happy to help debug. Sharing at least the root saga will get us started.

neurosnap avatar Jun 29 '22 14:06 neurosnap

@neurosnap Hey man, thank you! I missed your response. Sorry. I'll attach some code tomorrow when I'm working.

Roeefl avatar Jul 23 '22 17:07 Roeefl

any solution?

subhadeepquantiantech avatar Aug 25 '22 04:08 subhadeepquantiantech

You can't expect to get any help with an unknown problem - please share a runnable repro case so we can take a look and give you some advice.

Andarist avatar Aug 25 '22 05:08 Andarist

any workaround for this? I am also facing the same issue..

hafizSiddiq7675 avatar Sep 02 '22 15:09 hafizSiddiq7675

Hi @hafizSiddiq7675

Please provide a link to your source code or code sandbox replicating the behavior in order for us to best help.

neurosnap avatar Sep 02 '22 15:09 neurosnap

I'm also having this issue

UPDATE: solved. I think I also had duplicated root saga call

I

So, I am trying to dispatch an action to get a request. But I can see that the API is getting called twice but the action dispatched once. Please Help in this -

I did console.log() in both function forgetPasswordSaga and onForgetPasswordButtonClick Result : forgetPasswordSaga is called twice and onForgetPasswordButtonClick called once Component

const Login = (props: Props) => {
 const { dispatch } = props;

 const onForgetPasswordButtonClick = (data: ForgetPasswordData) => {
         dispatch({
           type: 'LOGINPAGE_FORGET_PASSWORD_REQUEST',
           payload: { data },
         });
      };
 
 const mapStateToProps = (state: any) => {
   const { loading } = state.ui;
   return {
     loading
   };
 };

 export default connect(mapStateToProps, null)(Login);

Saga

const forgetPasswordSaga = function* (action: Action) {
      try {
        const response = yield call(forgetPassword, action.payload);
        yield put({ type: FORGET_PASSWORD_SUCCESS });
        Router.go(RoutePath.LOGIN);
        return response;
      } catch (e) {
        yield put({ type: FORGET_PASSWORD_FAIL });
        yield put({ type: ERROR_SET, metadata: { message: Error.getErrorMessage(e) } });
      }
    };
    
    export default [
      takeLatest('LOGINPAGE_FORGET_PASSWORD_REQUEST', forgetPasswordSaga),
    ];

I think you also had duplicated root saga call, I also done that mistake on my rootsaga

Chanaka97n avatar Nov 28 '22 13:11 Chanaka97n

I am calling saga once but api calls are being made twice

vimal1824 avatar Dec 02 '22 04:12 vimal1824

here is the solution: import {shallowEqual} from 'react-redux';

pass it as a second parameter in your useselector

subhadeepquantiantech avatar Dec 02 '22 05:12 subhadeepquantiantech

Solved! The issue was with enhancers in createStore method, I believe it was executing saga middleware twice

vimal1824 avatar Dec 02 '22 08:12 vimal1824

Closing issue since this appears to be a context-specific issue. If anyone has an issue please open a new issue and I can help triage.

neurosnap avatar Dec 15 '22 15:12 neurosnap