REACT CONDITIONALS /// TERNARY OPERATOR /// LOGICAL AND /// AVOID THE ZERO PITFALL /// REACT CONDITIONALS /// TERNARY OPERATOR /// LOGICAL AND /// AVOID THE ZERO PITFALL ///

Dynamic React UIs

Master conditional rendering logic. Learn to show, hide, and switch components based on application state.

Component.jsx
1 / 9
12345
localhost:3000
Awaiting Render Instructions...

Tutor:Welcome! Let's make our React components smart enough to change what they display based on data.


Skill Matrix

UNLOCK NODES BY MASTERING CONDITIONAL LOGIC.

The Ternary (? :)

A JavaScript expression used for simple if-else logic inside JSX.

System Check

Why can't you use a standard 'if...else' block directly inside JSX {}?


Community Holo-Net

Peer Project Review

ACTIVE

Built a complex Dynamic UI? Submit your component logic for feedback from other Net-Runners.

Dynamic UIs & Conditional Rendering

Author

Pascual Vila

Senior Frontend Engineer // Code Syllabus

In React, building a static website is only half the story. The true power of React is in building dynamic user interfaces that respond to state, props, and user interactions.

Pattern 1: Ternary Operator (`? :`)

This is the most common and concise way to handle a simple `if-else` choice *inside* your JSX. It is a JavaScript expression, so it evaluates to a value and is perfectly valid inside ``.

function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <Dashboard /> : <LoginPage />}
    </div>
  );
}

Pattern 2: Logical AND (`&&`)

Use this when you want to render something *or nothing*. In JavaScript, `true && expression` evaluates to `expression`, and `false && expression` evaluates to `false`. React ignores `false` and renders nothing.

function Mailbox({ unreadMessages }) {
  return (
    <div>
      {unreadMessages.length > 0 && <h2>You have mail!</h2>}
    </div>
  );
}

⚠️ Pitfall: The `0` Problem

`0` is a falsy value. So 0 && >Component /< short-circuits and evaluates to `0`. Unlike `false`, React actually renders the number 0! Always use a boolean check like `count > 0`.

Pattern 3: Logical OR (`||`)

Perfect for default or fallback values. If the left side is falsy (like `null` or `undefined`), it renders the right side.

<h1>Hello, {user.name || 'Guest'}</h1>
Pattern 4: Element Variables+

If logic is too complex for ternaries, use a variable and standard `if/else` *before* the return statement.

let content;
if (status === 'submitting') content = <Spinner />;
else if (status === 'error') content = <ErrorIcon />;
else content = <SubmitButton />;

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

React Logic Glossary

Conditional Rendering

Outputting different JSX based on conditions, allowing for dynamic UIs.

logic.jsx

Ternary Operator (? :)

An inline if-else expression. Syntax: condition ? expressionIfTrue : expressionIfFalse.

logic.jsx

Logical AND (&&)

Used for short-circuiting. Renders the right side only if the left side is truthy.

logic.jsx

Logical OR (||)

Provides fallback values. Returns the left side if truthy; otherwise returns the right.

logic.jsx

Falsy

A value considered false in JS: false, 0, "" (empty string), null, undefined, NaN.

logic.jsx

Element Variable

Declaring a variable to hold JSX based on complex if/else logic before returning it.

logic.jsx