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

198
Vistas
More Efficient Way to Destructure with Conditional in ReactJs

I have this code in which we retrieve the data or get the data using useQuery. However, it fails anytime I attempt to call it using this method.

const { data: {getSubCategories} } = useQuery(FETCH_SUBCATEGORIES_QUERY);

It appears to throw an error stating that 'getSubCategories' is undefined, so I tried this method:

const { getSubCategories: subCategories } = { ...subCategoryData };
const { getChildCategory } = { ...data };

It works, but I think there is a better approach or more efficient way to destructure something with conditional syntaxes so it can't return undefined I guess I seem to notice it will take a few seconds before it will return data.

Would you mind commenting down below if you don't understand what I am trying to say or need more clarifications. Thank you.

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

0

It appears to throw an error stating that 'getSubCategories' is undefined

No, it's throwing an error when data is undefined. You can circumvent that by using a default:

const { data: {getSubCategories} = {} } = useQuery(FETCH_SUBCATEGORIES_QUERY);
about 4 years ago · Juan Pablo Isaza Denunciar

0

If I've understood correctly you can try this: (safely destruct from the object)

const { data: {getSubCategories} } = useQuery(FETCH_SUBCATEGORIES_QUERY) || {};

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