javascript - 如何使用 React Native 访问对象的属性?
问题描述
使用 React Native 时,无法访问brand
我的对象的属性,当然还有 less brand.name
。
有谁知道可能会发生什么?谢谢
代码:
<CardItem bordered style={ styles.cardItem }>
<Text style={{ padding:20 }} >{this.state.beer.brand.name}</Text>
</CardItem>
目的:
解决方案
您应该在直接访问嵌套键之前进行条件检查
就像是
const { beer } = this.state;
return(
<div>
<CardItem bordered style={ styles.cardItem }>
{beer && beer.brand && <Text style={{ padding:20 }} >{ beer.brand.name}</Text>}
</CardItem>
</div>
)
推荐阅读
- tensorflow - 无法计算 ConcatV2,因为输入 #1(从零开始)应该是一个浮点张量,但它是一个双张量 [Op:ConcatV2] 名称:concat
- django - 我想根据 id 分隔产品,但显示 tempatesyntaxterror
- c# - 如何在 Mac 和 Windows 上部署 .NET Framework 应用程序?如何构建跨平台(windows 和 MacOS).NET Core?
- python - Python检测系统默认小数分隔符(熊猫)
- hyperledger-fabric - 初始化核心配置时出现致命错误
- javascript - 如何在 GmailApp 中添加 2 个不同的谷歌驱动器文件夹的所有内容
- c++ - 内置 mod 运算符与 C++ 中的算术 while 循环
- node.js - 排行榜不和谐 js
- python - ValueError:数组太大...大于最大可能大小
- dart - 在飞镖中,如何让隔离从不同的包中创建子类对象的实例?