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

345
Vistas
¿Cómo filtrar useState hook Array y empujar y extraer datos a esa matriz?

Estoy buscando el filtro React useState hook array. Sin obtener ninguna respuesta. Por favor, ayúdame.

Defino la matriz de cadenas useState-

 const [filterBrand, setFilterBrand] = useState<string[]>([]);

Aquí está mi función para filtrar esta matriz y empujar, extraer datos:

 const brandHandler = (item: string) => { //Here I need if filterBrand already have the item then remove it and update the state, else add the item to this array and update the state }

Aquí necesito verificar si item ya está en filterBrand . luego elimínelo de filterBrand y actualice State . De lo contrario, necesito agregar el item a filterBrand y actualizar el State

Ya lo probé-

 const brandHandler = (item: string) => { const items: string[] = [...filterBrand]; if (!filterBrand.includes(item)) { items.push(item) } else { return items.filter(f => f !== item) } setFilterBrand(prevState => ({ ...prevState, ...items })); }

Pero obtener este error-

Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Hacer esto

 // setFilterBrand(prevState => [ ...prevState, ...items ]); would work setFilterBrand(items) // likely what you want instead

en vez de

 setFilterBrand(prevState => ({ ...prevState, ...items }));

Estás transformando tu matriz en un objeto por alguna razón aquí

Código completo:

 const brandHandler = (item: string) => { let items: string[] = [...filterBrand]; if (!filterBrand.includes(item)) { items.push(item) } else { items = items.filter(f => f !== item) } setFilterBrand(items); }
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