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

225
Visualizações
what is the right way to check that an object is not null in react before assigning one of its properties to a variable

In React it is very commong that as components get updated objects go from null to their intended content. This is a dynamic process that can create errors everytime you assing a property from those objects to another variable.

Lets say that user is the object that will go from null to the actual user object and that I want to extract the property uid from user:

const uid = user.uid

This can trigger a fatal error in react if user is null when I try to execute this operation.

I have tried to use:

user && const uid = user.uid, but it doesn't work at all.

Would the right way to do this be:

const uid = user && user.uid

any comments/ sugestions?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined

So do this

const uid = user?.uid // will be undefined if not there else the actually value
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