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

81
Vistas
Why does my useEffect run if I'm not expecting it?

I am making a search engine using an input and a button that I hope will execute the search but when I just enter a letter in the input that sets the search value of useState my useEffect is executed even though search is not found in the useEffect dependency array.

Input:

<div className="flex gap-1">
    <input className="border-1 border-2 border-solid rounded border-slate-300" value={search} onChange={(e) => setSearch(e.target.value)} />
    <button onClick={() => handleSearch()} <FiSearch/></button>
</div>

handleSearch()

const handleSearch = async () => { 
   setLimitPage(10)
   setPage(1)
   await getProducts(limitPage, page, search)
}

my useEffect that is executed without being expected that way as soon as search changes

useEffect (() => {
    // eslint-disable-next-line
    getProducts(limitPage, page, search) 
    setTotalDocs(products.products?.totalDocs)
    setTotalPages(products.products?.totalPages)
    setCurrentPage(products.products?.page)
    setNextPage(products.products?.hasNextPage)
    setPreviousPage(products.products?.hasPrevPage)

    const totalPages = Math.ceil(totalDocs/limitPage)
    const links = []
    for (let i = 1; i <= totalPages; i++) {
        links[i] = i
    }
    setNumbers(links)
}, [products, limitPage, page ])

I don't understand how I can avoid this so that I can use my search function before useEffect is executed

about 4 years ago · Juan Pablo Isaza
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