- Forums
- ionic
- Ionic Typescript React Application Useeffect Running Api Multiple Two Times Twice Duplicates
This Page Contains information about Ionic Typescript React Application Useeffect Running Api Multiple Two Times Twice Duplicates By useEffect in category ionic with 0 Replies. [5125], Last Updated: Mon Jun 24, 2024
useEffect
Mon May 22, 2023
0 Comments
269 Visits
I have a situation today, My Ionic App using Typescript with React was calling my API twice and creating multiple and duplicate records in my database. This could not work for me. I found a solution on how to run useEffect only once. This is what I had in my ogirinal code:
Problem
useEffect(() => {
console.log(`LINE 145`);
When I looked in my console, there were two LINE 145
Solution
import React, {useRef } from "react";
const fetchDataRef = useRef(false);
const showConsole = () => {
console.log(`LINE 145`);
}
useEffect(() => {
if (fetchDataRef.current) return;
fetchDataRef.current = true;
showConsole();
}, []);