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

282
Views
Cannot destructure property of '{}' as it is null

I've updated some legacy code to react 18 (from 17) and react-router-dom 6 (from v4) and for some reason I'm encountering this error on existing code (that was working just fine)

In all my components I access some data like so

const {
  user,
  person,
  loading,
  locale,
  building: { locale: building_locale } = {},
  updateContext,
} = useContext(GlobalContext)

Right here is the console of said context on his initial state, on the render I'm getting the issue below : enter image description here enter image description here

The code works perfectly fine if I'm doing some workaround like this

const {
  user,
  person,
  loading,
  locale,
  building,
  updateContext,
} = useContext(GlobalContext)
const building_locale = building?.locale

But those changes would impact roughly 3k files and I'm really not looking forward to having to execute that kind of regex.

As far as I know, the code is actually working fine and the error makes no sense but maybe someone has any clue as to why this might happen

I would have provided a codepen if I was somehow able to reproduce in a pristine coding environment but this seems to be specific to my app's context, mostly looking for some ideas / rubber ducks 🥲

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There seem to be an issue when building value is null, a workaround is to declare it as undefined instead:

const o1 = { building: { locale: undefined } };  // Works - even with null
const o2 = { building: null };  // Throws
const o3 = { building: undefined };  // Works


const testDestructuring = (obj) => {
  try {
    const { building: { locale: building_locale } = {} } = obj;
    console.log(JSON.stringify(obj),': Destructured with no errors');
  } catch (e) {
    console.log(e);
  }
};

const tests = [o1, o2, o3];
tests.forEach(testDestructuring);

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!