Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
UseEffect hook in next.js is causing 2 fetch requests when I expect 1, why?

The following code is causing 2 requests to be made to my API with the GET endpoint. I don't understand useEffect() enough to know where to start to debug, but I don't want to cause unneccessary strain on the services that these API endpoints will call server side. This is because they will be calling windows servers to action delete requests in active directory etc...

function Ad() {
    const [data, setData] = useState(null)
    const [isLoading, setLoading] = useState(false)
    useEffect(() => {
        setLoading(true)
        fetch('../api/decom/ad')
            .then(response => response.json())
            .then(data => {
                setData(data)
                setLoading(false)

                if (data.exists == true) {
                    Remove()
                    }
            })
    }, [])

    const Remove = () => {

        fetch("../api/decom/ad", { method: "DELETE" })
            .then(response => response.json())
            .then(data => {
                var inner = ""
                if (data.removed == true) {
                    inner = `<p>Removed: ${greenCircle}</p>`
                } else {
                    inner = `<p>Removed: ${redCircle}</p>`
                }

                document.getElementById('adremoved').innerHTML = inner

            })
    }

    if (data != null  && data.exists == true) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <p>Exists: { greenCircle } </p>
            <div id ="adremoved" className={styles.parent}>
                <p className={styles.child}>removed:</p>
                <div><div className={styles.loader}><div></div><div></div><div></div></div></div>
            </div>
        </a>
    )

    if (data != null && data.exists == false) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <p>Exists: {redCircle} </p>
            <p>Removed:{redCircle}</p>
        </a>
        )

    if (isLoading) return (
        <a className={styles.card}>
            <h2>AD</h2>
            <div className={styles.parent}>
                <p id="adstatus" name="adstatus" className={styles.child}>Exists:</p>
                <div id="adloader"><div className={styles.loader}><div></div><div></div><div></div></div></div>
            </div>
            <p>Removed:{redCircle}</p>
        </a>
        )
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

const [isLoading, setLoading] = useState(true)
    useEffect(() => {
       if(isLoading){
        fetch('../api/decom/ad')
            .then(response => response.json())
            .then(data => {              
                if (data.exists == true) {
                    Remove()
                    }else{
                setData(data)                
                }
               setLoading(false)
            })
       }
    }, [isLoading])
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda