React Conditional Rendering Examples

react conditional rendering

React conditional rendering is a way to show different parts of a page. Click here to see how it works and see practical examples for real projects.

What is Conditional Rendering in React?

Conditional rendering in React means the view shows one part or another based on data.

The component reads a condition and then shows a result that matches it.
It works like an if statement in normal JavaScript, but inside JSX.

Here is the syntax:

{condition ? <ComponentA /> : <ComponentB />}

This code shows ComponentA when the condition is true and shows ComponentB when false.

You need conditional rendering to show different layouts for user states or data states. It keeps the code simple and stops many duplicate components on the page.

Here is a quick example:

function UserStatus({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <p>Welcome back</p> : <p>Please log in</p>}
    </div>
  );
}

This code checks if the user is logged in and then shows a message that matches the state.

The Different Ways to do Conditional Rendering in React

React gives many ways to handle conditions inside components.
You can use ternary operators, logical AND, switch cases, or separate functions.

Logical AND:

{isLoggedIn && <Dashboard />}

This line shows Dashboard only when isLoggedIn is true.

Switch:

switch(status) {
  case 'loading':
    return <Spinner />;
  case 'done':
    return <Content />;
  default:
    return <Fallback />;
}

This code changes the view based on a status value.

Use Functions for Conditional Rendering

You can move conditions into a function to keep JSX clean.

function renderContent(user) {
  if (user.admin) return <AdminPanel />;
  return <UserPanel />;
}

function App({ user }) {
  return <div>{renderContent(user)}</div>;
}

This approach keeps logic outside JSX and makes each part easy to read.

Examples

Show Admin Panel or User Panel:

function App({ user }) {
  return (
    <div>
      {user.isAdmin ? <AdminPanel /> : <UserPanel />}
    </div>
  );
}

This code checks if a user is an admin and then shows the right panel for that state in the app.

Load Spinner before Content:

function ContentLoader({ loading }) {
  return (
    <div>
      {loading ? <Spinner /> : <Content />}
    </div>
  );
}

This example shows a spinner before the content and then shows the content after the data loads from the server.

Multiple Status Handling:

function StatusView({ status }) {
  switch (status) {
    case 'idle':
      return <p>Idle state</p>;
    case 'loading':
      return <p>Loading...</p>;
    case 'done':
      return <p>Done!</p>;
    default:
      return <p>Unknown</p>;
  }
}

This code switches between many states and shows the correct message or view for each one.

Function-based Render with Nested Components:

function renderDashboard(user) {
  if (user.role === 'editor') return <EditorDashboard />;
  if (user.role === 'viewer') return <ViewerDashboard />;
  return <GuestDashboard />;
}

function App({ user }) {
  return <div>{renderDashboard(user)}</div>;
}

This example moves the decision into a function and gives a dashboard that fits each user role.

Wrapping Up

You learned what conditional rendering is in React and how to apply it in many ways. You saw how to use ternary operators, logical AND, switch, and functions.

Here is a quick recap:

  • Conditional rendering lets you show different components for different states and keeps your code short.

FAQs

What is React conditional rendering?

React conditional rendering means React shows or hides parts of the UI based on logic. You can use if statements, ternary operators or ;&& expressions to decide what to display.

How do you do conditional rendering in React?

You can use multiple methods:
  • Use if statement inside a function:

function MyComponent() {
  const show = true;
  if (show) {
    return <div>Visible content</div>;
  }
  return <div>Hidden content</div>;
}
  • Use ternary operator:

{ show ? <div>Visible</div> : <div>Hidden</div> }

What are common patterns of conditional rendering in React?

Common patterns:
  1. Use if statement inside render function
  2. Use ternary operator directly in JSX
  3. Use && short-circuit evaluation
  4. Use separate components for different states

Can React conditional rendering affect performance?

Yes. Complex logic or nested conditions can slow render cycles. Break conditions into smaller functions or memoize parts to reduce re-renders.

Similar Reads

React Components: A Complete Tutorial for Beginners

In this tutorial, you will learn what components are in React and how to build them. Also, how to pass…

Forms in React: How to Create and Manage User Inputs

Forms in React allow a page to take data from a user. A developer uses them to show fields, take…

React JS Introduction: What is React? the JavaScript Library

In this introduction, you will understand what React JS is and learn how to use NPM commands to get started…

React Lists and Keys: Manage Dynamic Elements

React Lists and Keys help you show many items and keep each item tracked for updates. What do Lists and…

Events in React: A Complete Guide for Beginners

Events in React are actions that a user does with a page. A user can click a button or type…

Learn JSX in React: Syntax, Rules, and Examples

JSX helps write React UI with HTML-like syntax. It links JavaScript logic with markup in one view. What is JSX…

State in React Step by Step with Examples

State in React lets a component hold data that can change over time. You can use it to track values…

How to Install React on Ubuntu, Windows and MacOS

Through this article, you will learn how to install React on common operating systems such as Windows, macOS, and Ubuntu.…

How to Render HTML Elements in React.js

Rendering HTML in React might sound like a complicated task, but it’s actually quite simple once you understand the basics.…

Props in React explained with examples

Props in React pass data from one part to another part. They let you build parts that reuse data and…

Previous Article

Node.js Streams Guide: How to Handle Data with Examples

Next Article

SQL Server Data Types: The Complete Guide with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.