how to use if else in jsx 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: ternary react
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
);
}
Example 4: adding a if stement in jsx
render() {
return (
{this.state.value == 'news'? data: null }
)
}
Example 5: make a if in jsx
var loginButton;
if (loggedIn) {
loginButton = ;
} else {
loginButton = ;
}
return (
);