0

On button click my redux action is getting dispatched twice. I have tried many possible ways but still no solution.

In Component:

<button type="button" onClick={this.props.submitRequest} className="btn btn-primary btn-md" style={{flexBasis:'100%'}}>Send Request</button>

const mapDispatchToProps = dispatch => ({
    submitRequest: (payload) => dispatch(actions.submitRequest(payload)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Pop);

In actions:

export const submitRequest = (payload) => ({
    type: 'REQUEST',
    payload
});

In Saga:

export function* request() {
  console.log('saga start')
}

export default function* rootsaga() {
  yield takeEvery('REQUEST', request);
}

Everytime i am getting 'saga start' message twice on my console.

srk_pathan
  • 396
  • 2
  • 10
  • 1
    You can erroneously started saga twice. Look at where `saga.run` is used and check if it starts `rootsaga` twice. – Fyodor Yemelyanenko Jan 30 '20 at 06:43
  • No, i have run it only once. – srk_pathan Jan 30 '20 at 06:47
  • I tried to make same sample it is working fine and the only thing which call twice is button click twice so in this case you can either disable button after click or you can use in saga takelatest() in place of takeEvery – Niraj Jan 30 '20 at 08:23
  • @Niraj TakeLatest is also giving the same result and button is getting pressed once – srk_pathan Jan 30 '20 at 09:12
  • Can you please try to log in root saga like how many times it is called? and also add code where you add root saga? – Niraj Jan 30 '20 at 10:22
  • i have checked all the files and there is only one location where i am starting it. i have logged in button click event and that is also being called once.So, not sure where is the issue. – srk_pathan Jan 30 '20 at 11:16
  • I checked whole code again and got to know that i was starting saga twice, due to that i was facing saga execution twice. Thanks @Fyodor and Niraj for help. – srk_pathan Jan 31 '20 at 10:40

0 Answers0