react native conditional rendering

Boss Nass picture Boss Nass · Feb 7, 2017 · Viewed 28.4k times · Source

I am trying to use an inline if statement to check if a piece of data exists and if it does to display it. this code is currently sitting in my render, return block.

the problem I am having is that using this, the content is no longer being rendered

{(() => {
              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)} />
                ))}
              }
            })()}

Answer

Ata Mohammadi picture Ata Mohammadi · Feb 7, 2017

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

There you go.