Skip to content
Advertisement

API called twice while useEffect triggered once. ReactJS/Javascript

i am building a project, in which when user click the buyNow button in Basket (child 2) it will pass the props back to parent where it further pass it to another child in Signin(child 3) where we call an API call(inside useEffect) to update the mysql database but it seems that the API call is called twice as in database two records are being created and in front end i got two identical invoices record but different file names.

Any suggestion guys why i am facing this, please note if i remove the useEffect statement from the Signin it will then continuously non-stop call the API call so i think i cant remove the useEffect, other then this i cant see why it is happening. any suggestion please.

JavaScript

Update-1: After removing the strictMode it does solve the issue, but does it means if i temporarily fixed the issue or if i have to use the stricMode and find the real problem

  • Child 2- Basket– Customer press the buyNow button and it triggers resetBasket function
JavaScript
  • Main App resetBasket takes the basketitems and pass to parent element
JavaScript
  • Child 3 – Signin ,here we call the API call(using useEffect) and update the Mysql server and recieve the invoice in PDf format from backend
JavaScript
  • Sub Child-Useraccount then it display the items recieved from the backend-mysql nodejs
JavaScript

Advertisement

Answer

I think the <React.StrictMode> is causing double execution. Please take a look after removing it from your top level component. Please refer This Link for more details on StrictMode double execution.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement