Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda