refactor(typesContext.tsx): remove unnecessary retry logic and interval for fetching types

fix(typesContext.tsx): fix issue where component was not unmounting properly and causing memory leaks
This commit is contained in:
anovazzi1 2023-08-23 14:44:45 -03:00
commit ea105ee7af

View file

@ -38,11 +38,6 @@ export function TypesProvider({ children }: { children: ReactNode }) {
const { setLoading } = useContext(alertContext);
useEffect(() => {
let delay = 1000; // Start delay of 1 second
let intervalId = null;
let retryCount = 0; // Count of retry attempts
const maxRetryCount = 5; // Max retry attempts
// We will keep a flag to handle the case where the component is unmounted before the API call resolves.
let isMounted = true;
@ -85,22 +80,13 @@ export function TypesProvider({ children }: { children: ReactNode }) {
}, {})
);
}
// Clear the interval if successful.
clearInterval(intervalId);
} catch (error) {
console.error("An error has occurred while fetching types.");
}
}
// Start the initial interval.
intervalId = setInterval(getTypes, delay);
return () => {
// This will clear the interval when the component unmounts, or when the dependencies of the useEffect hook change.
clearInterval(intervalId);
// Indicate that the component has been unmounted.
isMounted = false;
};
getTypes();
}, []);
function deleteNode(idx: string) {