React - conditional operator

0 votes
246 views
added Mar 22, 2023 in React by lcjr First Warrant Officer (11,790 points)

In React, you can use the conditional operator (condition) ? (expression if true) : (expression if false) or the logical && operator to conditionally render elements in your JSX code.

Here's an example of how to use the conditional operator:

import React from 'react';

function MyComponent({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? (
        <p>Welcome, user!</p>
      ) : (
        <p>Please log in to continue.</p>
      )}
    </div>
  );
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...