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

144
Views
useState has previous value instead of the current

I'm new to react and I'm trying to understand how it's working.

I have 2 select inputs and on change event I want to filter the other's select values based on the first one.

After calling setOptions with the filtered values, they are always one step behind - so I have the previous value each time.

I understand that useState is async and I tried with and useEffect (() => {}, [options]) and I still have the previous values in options.

 const [options, setOptions] = useState<any>()
 const filterOptions = (ids: string[]) => {
        const filteredOptions = list.filter(item=> {
            return list.includes(item.id)
        })
        setOptions(filteredOptions)
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think your primary problem is from the logic itself. list.includes(item.id) is checking item.id on list that always returns true.

You can modify it to ids.includes(item.id)

 const [options, setOptions] = useState<any>()
 const filterOptions = (ids: string[]) => {
        const filteredOptions = list.filter(item => {
            return ids.includes(item.id)
        })
        setOptions(filteredOptions)
    }

Shorter version

 const [options, setOptions] = useState<any>()
 const filterOptions = (ids: string[]) => {
        const filteredOptions = list.filter(item => ids.includes(item.id))
        setOptions(filteredOptions)
    }
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!