question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why API is getting called twice in redux-saga?

See original GitHub issue

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),
    ];

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
iamrahmancommented, Aug 23, 2020

@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
  ]);
2reactions
mastercodebegincommented, Mar 19, 2022

@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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why API is getting called twice in redux-saga? - Stack Overflow
So, I am trying to dispatch an action to all a get request. But I can see that the API is getting called...
Read more >
How to prevent redux-saga from calling my api call twice?
Coding example for the question How to prevent redux-saga from calling my api call twice?-Reactjs.
Read more >
Running Tasks In Parallel | Redux-Saga
wrong, effects will be executed in sequence const users = yield call(fetch, ... Because the 2nd effect will not get executed until the...
Read more >
Control when Saga Generators are Called with TakeLatest
[01:09] If there's already an instance of the generator function running when it sees this dispatched type, it will cancel it and start...
Read more >
Multiple api calls even for single saga action?
I was facing this issue in my React app and I could not find the exact reason in internet. There were many hacks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found