Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!