反应原生条件渲染

时间:2017-02-07 11:09:12

标签: if-statement react-native conditional render

我正在尝试使用内联if语句来检查是否存在一段数据以及是否显示它。此代码当前位于我的渲染,返回块中。

我遇到的问题是使用它,内容不再呈现

{(() => {
              if (this.props.data.size) {
                <Text style={styles.headerLabel}>Sizes</Text>
                {(this.props.data.size||[]).map((section,i) => (
                  <AddToCartRow key={i} data={section} productName={this.props.data.name} value={Config.priceToPriceWithCurrency(section.price)} />
                ))}
              }
            })()}

2 个答案:

答案 0 :(得分:24)

render(){
  return(
    <View>
    {this.state.error && <Text style={{ color: 'red' }}>{this.state.errorMessage}</Text>}
    <Text>Hello World!</Text>
    </View>
  );
}

你去。

答案 1 :(得分:2)

下面的代码也检查空字符串。

render(){
  return(
    <View>
    {!!this.state.error && <Text>{this.state.errorMessage}</Text>}
    </View>
  );
}
相关问题