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

250
Vistas
Cannot read properties of undefined (reading 'some_props')

I have a problem with React conditional rendering. I want to render a specific prop if it's set so first I'm checking if the prop exists and then outputing it otherwise output 0, but React has a problem checking if the variable exists when it's undefined at first load...

number:
    props.dashboardCards[0].items.thisMonth[0].itemTitle
      ? props.dashboardCards[0].items.thisMonth[0].itemTitle
      : 0,

This is my code I want to create an object with number field and in that field I want to set this prop if it exists otherwise 0. And React keeps giving me error:

TypeError: Cannot read properties of undefined (reading 'itemTitle')

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

thisMonth is probably empty or undefined. You can use optional chaining to solve this:

number: props.dashboardCards[0]?.items?.thisMonth[0]?.itemTitle ?? 0,

Now if props.dashboardCards[0].items.thisMonth[0].itemTitle exists number will be the value of it, or it'll be 0.

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

Learn more about optional chaining from here.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The error says that there is no itemTitle in the thisMonth array.

If it's coming from the API, you need to wait to get it. You can simply check the props by the console.log(props) and check it before using dot notation to get the itemTitle.

You can also check for a value with optional chaining ? :

props?.dashboardCards[0]?.items?.thisMonth[0]?.itemTitle

It means:

if(props){
  if(dashboardCards[0]){
    if(items){
      if(thisMonth[0]){
        props.dashboardCards[0].items.thisMonth[0].itemTitle
      }
    }
  }
}
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