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

173
Vistas
How can I remove type 'false' inside a setState generic used in react?

Hi I'm trying to use useState and handling a state with generics. Inside a setState, I manipulated a fetched array with filter function and as you know it returns false.

so... I'll show my codes.

 const [mapItemsState, setMapItems] = useState<{ [k: string]: number | string }[]>([]);

 setMapItems(
     Object.entries(data)?.map(
        ([key, value]) =>
               Object.keys(mapItemListKor).includes(key) && {
               [key]: value as string | number,
        },
     )
     .filter(Boolean)        // I erased a false here!
 )

and this code returns this error Argument of type '(false | { [x: string]: string | number; })[]' is not assignable to parameter of type 'SetStateAction<{ [k: string]: string | number; }[]>'.

I removed false using filter but it still detect false.

How can I handle it?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

 const [mapItemsState, setMapItems] = useState<{ [k: string]: number | string }[]>([]);

 setMapItems(
     Object.entries(data).filter(Boolean)?.map(
        ([key, value]) =>
               Object.keys(mapItemListKor).includes(key) && {
               [key]: value as string | number,
        },
     )
 )

The issue you were having is that you were not filtering the array before it got mapped, meaning that the falses still get passed into the .map

about 4 years ago · Santiago Gelvez Denunciar

0

Anyway I solved this problem as below.

setMapItems(
                Object.entries(data)
                    .filter(([key]) =>
                        Object.keys(mapItemListKor).includes(key),
                    )
                    .map(([key, value]) => {
                        return { [key]: value as string | number };
                    }),
            );
about 4 years ago · Santiago Gelvez 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