conditional statement in jsx code example

Example 1: how to use if else inside jsx in react

renderElement(){
   if(this.state.value == 'news')
      return <Text>data</Text>;
   return null;
}

render() {
    return (   
        <View style={styles.container}>
            { this.renderElement() }
        </View>
    )
}

Example 2: react native conditional rendering

class Component extends React.Component {
	const a = true
	render() {
      return (
    	<Container>
          {a == true
           ? (<Button/>)
           : null
          }
        </Container>    	
	  )
	}
}

This is staying: if a == true, render a button component. 
Otherwise render null, in other words nothing.

Example 3: adding a if stement in jsx

render() {
    return (   
        <View style={styles.container}>
            {this.state.value == 'news'? <Text>data</Text>: null }
        </View>
    )
}

Example 4: react if statement

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.    </div>
  );
}

Tags:

Html Example