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

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

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 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