OscarDev
OscarDev

Reputation: 977

How to access property on an object with React Native?

Working with React Native, unable access the brand property of my object and of course less brand.name.

Does anyone know what might be happening? Thank you

Code:

<CardItem bordered style={ styles.cardItem }>
    <Text style={{ padding:20 }} >{this.state.beer.brand.name}</Text>
 </CardItem>

Object:

object on console

Upvotes: 2

Views: 2897

Answers (1)

Hemadri Dasari
Hemadri Dasari

Reputation: 33994

You should do conditional check before accessing nested keys directly

Something like

  const { beer } = this.state;
 return(
     <div>
         <CardItem bordered style={ styles.cardItem }>
              {beer && beer.brand && <Text style={{ padding:20 }} >{ beer.brand.name}</Text>}
         </CardItem>
     </div>
    )

Upvotes: 4

Related Questions