Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda