What is conditional rendering in ReactJS | How to use Conditional Rendering in React Js | React Conditional Rendering Explained with Examples

Image 177 views

Conditional Rendering in ReactJS is the technique of showing or hiding parts of the UI based on certain conditions β€” like whether a user is logged in, a form has been submitted, or data is loading. It’s similar to how we use if, else, or ternary operators in JavaScript, but within JSX syntax. This makes React applications flexible and user-friendly.

In React, you can conditionally render elements using:

  • If/else statements
  • Ternary operators πŸ™‚
  • Logical AND (&&) operator

This concept is extremely helpful when you want to toggle components, display alerts, hide or show elements, or even redirect users based on state or props.

πŸ‘‰ Download Free Ebook β†’

πŸ‘‰ Get Free Course β†’

Image

In this article, we’ll build a simple component that checks whether a user is logged in. Depending on the state, the component will show either a “Welcome Back!” message or a prompt to log in.

βœ… Example: Login Toggle using Conditional Rendering

🧩 React Functional Component (HTML + JavaScript)

  1.   import React, { useState } FROM 'react';
  2.  
  3. FUNCTION ConditionalRenderApp() {
  4.   const [isLoggedIn, setIsLoggedIn] = useState(FALSE);
  5.  
  6.   const toggleLogin = () => {
  7.     setIsLoggedIn(!isLoggedIn);
  8.   };
  9.  
  10.   RETURN (
  11.     <div STYLE={{ padding: '20px', fontFamily: 'Arial' }}>
  12.       <h2>πŸ” Conditional Rendering Example</h2>
  13.  
  14.       {/* Conditional Message */}
  15.       {isLoggedIn ? (
  16.         <p>Welcome back, <strong>Vijay Kumar</strong>! πŸŽ‰</p>
  17.       ) : (
  18.         <p>Please login TO continue.</p>
  19.       )}
  20.  
  21.       {/* Toggle Button */}
  22.       <button onClick={toggleLogin} STYLE={{ padding: '10px 20px', marginTop: '10px' }}>
  23.         {isLoggedIn ? 'Logout' : 'Login'}
  24.       </button>
  25.     </div>
  26.   );
  27. }
  28.  
  29. export DEFAULT ConditionalRenderApp;

 

πŸ’‘ Key Points:

  • Ternary operator is used here for clean JSX rendering.
  • You can also use && for short conditional logic like {isLoggedIn && Logged In!}
  • The button toggles login state using useState.

 

FAQ (Frequently Asked Questions)

What is the purpose of conditional rendering in React?

In React, conditional rendering is the process of displaying different content based on certain conditions or states. It allows you to create dynamic user interfaces that can adapt to changes in data and user interactions. In this process, you can use conditional statements to decide what content should be rendered.

What is the difference between conditional rendering and dynamic rendering?

Dynamic rendering involves changing the content based on variables, while conditional rendering controls whether elements appear or not. Using these tools effectively can help you create flexible, responsive, and interactive user interfaces.

What is rendering in React?

Rendering is the process of React asking your components to describe what they want the UI to look like, based on their current props and state. And then taking that and applying any necessary updates to the DOM.

 
  
πŸ‘‰ Get Free Course β†’ Image

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

Image

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Image

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net