React Conditional Rendering

In React, you can use JavaScript conditional statements to dynamically render components or elements.

The following are several common methods for handling conditional rendering in React:

1. Using if statements

Use if statements in the render method or in the return value of function components to decide what to render.

Example

import React from 'react';
import ReactDOM from 'react-dom/client';

class MyComponent extends React.Component {
  render() {
    const isLoggedIn = this.props.isLoggedIn;
    let content;

    if (isLoggedIn) {
      content = <h1>Welcome back!</h1>;
    } else {
      content = <h1>Please sign up.</h1>;
    }

    return (
      <div>
        {content}
      </div>
    );
  }
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

2. Using the ternary operator

In JSX, you can use the ternary operator for concise conditional rendering.

Example

import React from 'react';
import ReactDOM from 'react-dom/client';

const MyComponent = (props) => {
  const isLoggedIn = props.isLoggedIn;

  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

3. Using the logical AND (&&) operator

In JSX, you can use the logical AND operator for conditional rendering. If the condition is true, the following element is rendered.

Example

import React from 'react';
import ReactDOM from 'react-dom/client';

const MyComponent = (props) => {
  const isLoggedIn = props.isLoggedIn;

  return (
    <div>
      {isLoggedIn && <h1>Welcome back!</h1>}
      {!isLoggedIn && <h1>Please sign up.</h1>}
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

4. Using switch statements

When you need to handle multiple conditions, you can use a switch statement in the render method.

Example

import React from 'react';
import ReactDOM from 'react-dom/client';

class MyComponent extends React.Component {
  render() {
    const userRole = this.props.userRole;
    let content;

    switch (userRole) {
      case 'admin':
        content = <h1>Welcome, Admin!</h1>;
        break;
      case 'user':
        content = <h1>Welcome, User!</h1>;
        break;
      case 'guest':
        content = <h1>Welcome, Guest!</h1>;
        break;
      default:
        content = <h1>Who are you?</h1>;
    }

    return (
      <div>
        {content}
      </div>
    );
  }
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent userRole="admin" />);

Summary

  • ifstatement: suitable for use inrendermethods or the return value of function components to determine the rendering content.
  • ternary operator: suitable for concise conditional rendering in JSX.
  • logical AND (&&) operator: suitable for conditional rendering in JSX. When the condition istruethen render the element.
  • switchstatement: suitable for handling multiple conditions and rendering different content.
Other Extensions