Yogesh Chauhan's Blog

What is Conditional Rendering in React?

in React on May 4, 2021

Conditional rendering is nothing but rendering based on some conditions like if-else conditions in JavaScript.

React is component based so we can have different components and add some conditions to render them only when the condition is satisfied.

In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.

Example

Let’s add couple of components:

In the code above, we have two components. We want them to be rendered differently based on the hour of the day. Here’s how we can do that using some JavaScript code in React.


How to store elements in variables?

We can store elements in variables and then render just some part of the component.

We can create a stateful component that will render depending on its current or previous state.

Source: React Docs


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to send and receive query strings via links in Angular 9?Angular3 JavaScript methods to use when you want to go back and forth in historyJavaScript2 Ways We Can Write Multiple Line Commands in PHPPHP3 Ways we can create URLSearchParams Objects in JavaScriptJavaScriptFew more :nth-child examples in CSSCSSPHP __construct() functionPHPHow to make a curtain slider using jQuery and CSS?CSSWhat are Null Sessions?MiscellaneousHow to use data-* Attributes in HTML?HTMLHow to get front page or home page ID in WordPress?WordPressUnary Operators in JavaScriptJavaScriptSorting Arrays in JavaScriptJavaScript