react native if condition in render code example

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

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

render() {
    return (   
        
            { this.renderElement() }
        
    )
}

Example 2: ternary react

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

Example 3: react native conditional rendering

function Mailbox(props) {
  const unreadMessages = props.unreadMessages;
  return (
    

Hello!

{unreadMessages.length > 0 &&

You have {unreadMessages.length} unread messages.

}
); }

Tags:

Misc Example