Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda