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

172
Views
useEffect doesn't work correctly even when setting the dependency

I have a useEffect function filters out two arrays but for some reason it isn't working correctly. If I remove the dependency array then the brings back the correct data but it keeps on looping. enter image description here

I've then added the dependency array back in, but then it comes back empty. Any ideas as to where I'm going wrong?

useEffect(() => {
  if (extras.length && pkg ? .id) {
    setVisibleExtras(
      extras.filter(({
          availableOnTariff
        }) =>
        availableOnTariff.some(({
          id
        }) => id === pkg.id)
      )
    );
    console.log('visible extras', visibleExtras);
  }
}, [extras, pkg]);
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

If setVisibleExtras changing extras it will always trigger whole useEffect again... if its your case, use callback function in setVisibleExtras instead:

useEffect(() => {
  if (extras.length && pkg ? .id) {
    setVisibleExtras((extras) =>
      extras.filter(({
          availableOnTariff
        }) =>
        availableOnTariff.some(({
          id
        }) => id === pkg.id)
      )
    );
    console.log('visible extras', visibleExtras);
  }
}, [pkg]);
about 4 years ago · Santiago Gelvez 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!