Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!