React Conditional Rendering
React Conditional Rendering
In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React component updates the UI to match them.
There are several ways to do Conditional Rendering in React that are as follows:
- If
- Ternary operator
- Logical && operator
- Switch case operator
- Conditional Rendering with enums
if
It is the simplest way to do a conditional rendering in React. It does not apply to the total block of the component. If the condition is true, it will return the rendered element. It can be clear with the following example:
Example:
function UserLogin(props) { returnHello World!!
; } function GuestLogin(props) { returnSign Up First.
; } function SignUp(props) { const isLoggedIn = props.isLoggedIn; if (isLoggedIn) { return; } return ; } ReactDOM.render( , document.getElementById('root') );
Logical && Operator
It is a Logical && (AND) operator, which is also used for condition check. If you are using it with if operator, then it checks both conditions, i.e., if the first condition will true, then it will check the second condition, but if the first condition is false, then it will skip the second condition. It happens because AND (&&) operator will give you the output only when both of the conditions are true.
If you are not using it with if the operator and treat the second statement as an output statement, then, you will only get the output when your first conditional statement is true. If your conditional statement is false, then it will skip your second statement.
Example:
The following example will show you the use of && (AND) operator with if operator:
import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { render() { if((10 > 5) && (5>4)) { return(); } } } export default App;Hello
Output:
Another example of AND (&&) operator in which the second statement works as output statement:
import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { render() { return(<div> { (8 > 4) && alert('!!!! Hello World !!!!') } </div> ); } } export default App;
Output:
Ternary Operator
This operator makes the if-else statement much brief. This ternary operator is used when two alternate blocks give a particular condition.
The syntax of the ternary operator is as follows:
Syntax:
condition ? true : false
If the condition is true, then statement1 (true statement) will beconsidered as a result, but if the condition is false, then the statement2 (false statement) will be considered as a result.
It will be clear will the following example:
Example:
- When the condition is true, it will render statement1.
import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { render() { return (); } } export default App;{10>5 ? 'Condition is True' : 'Condition is False'}.
Output:
- When the condition is false, then it will render statement2.
import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { render() { return (); } } export default App;{10<5 ? 'Condition is True' : 'Condition is False'}.
Output:
Switch case Operator
This operator is used when you have multiple conditional renderings.
The example of switch case is as follows:
Example:
import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { render() { var i=2; switch(i) { case 0: return; case 1: returnSunday
; case 2: returnMonday
; case 3: returnTuesday
; case 4: returnWednesday
; case 5: returnThursday
; case 6: returnFriday
; default: return null; } } } export default App;Saturday
Output:
Conditional Rendering with Enums
Enums is the short word for ‘enumerations,' and a right way for the multiple conditional rendering. It provides more readability than the switch-case operator. It is a suitable option for mapping between different states and also mapping in more than one condition.
Example:
function NotificationMsg({ text, state }) { return ({{ info:); }, warning: , }[state]}
Example of Conditional Rendering
The below example will show you the use of conditional rendering.
Example:
import React, { Component } from 'react'; // Message Component function Message(props) { if (props.isClicked) return; else return
!!!! Hello World !!!! ; } // Click Component function Click(props) { return(
!!! It is an example of Conditional Rendering in React!!! ); } // Clicked Component function Clicked(props) { return( ); } class App extends Component{ constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.handleClicked = this.handleClicked.bind(this); this.state = {isClicked : false}; } handleClick() { this.setState({isClicked : true}); } handleClicked() { this.setState({isClicked : false}); } render(){ return( ); } } export default App;
Conditional Rendering Example { (this.state.isClicked)?( ) : ( ) }
Output:
Once the above code gets successfully compiled, you will get the following output:
After clicking the button, you will get the following output: