if statement inside html react code example
Example 1: if else render react
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
{isLoggedIn ? (
) : (
)}
);
}
Example 2: how to use if else inside jsx in react
renderElement(){
if(this.state.value == 'news')
return data;
return null;
}
render() {
return (
{ this.renderElement() }
)
}
Example 3: make a if in jsx
var loginButton;
if (loggedIn) {
loginButton = ;
} else {
loginButton = ;
}
return (
);
Example 4: conditional rendering react
function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
Hello!
{unreadMessages.length > 0 && You have {unreadMessages.length} unread messages.
}
);
}
const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(
,
document.getElementById('root')
);