Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda