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

205
Visualizações
React useEffect: is it possible to set dependencies with a variable?

I'm calling a function to determine if a set of form fields are valid. I have a set of state variables that should trigger useEffect() but apparently I can't make an array beforehand like this without getting a warning:

const states = [name, email, password]

useEffect(() => {
  someFunction({ states })
}, states)

Instead I have to do this:

useEffect(() => {
  someFunction({ states: [name, email, password] })
}, [name, email, password])

This is annoying because I plan on using this pattern on other forms that will have 10-20 fields, so I hate to clutter it up by building that whole array every time. Is there a better way?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Technically, yes, it's possible. What you're seeing is a warning from eslint, and you can disable it in a line/file basis. Check the docs here: https://eslint.org/docs/user-guide/configuring/rules#disabling-rules

const states = [name, email, password]

useEffect(() => {
  someFunction({ states })
}, states) // eslint-disable-line <eslint-error-id>

It's rarely a good idea to have a variable like that, as eslint won't be able to tell you if you're missing a dependency.

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