useEffect(() => { setTimeout(async () => { await fetch(url) .then((res) => res.json()) .then((data) => { if (Object.keys(data).length !== 0) { setIsLoaded(true); setRoundTrip(data); } else { Swal.fire({ icon: "error", title: "Oops...", text: "No Flights Found", confirmButtonText: "Search Again...", }) .then(function () { navigate("/"); }); } }); }, 1000); }, [url]);Me da el mensaje de error varias veces, pero quiero mostrar el mensaje de error solo una vez, entonces debería dejar de buscar.
Puede establecer condiciones para res.status.
useEffect(() => { setTimeout(async () => { await fetch(url) .then((res) => res.json()) .then((data) => { //here is solution if(res.status === 500){ Swal.fire({ icon: "error", title: "Oops...", text: "No Flights Found", confirmButtonText: "Search Again...", }) .then(function () { navigate("/"); }); } if (Object.keys(data).length !== 0) { setIsLoaded(true); setRoundTrip(data); } }); }, 1000); }, [url]);