All posts
ReactSeptember 19, 20267 min read

React Conditional Rendering for Beginners (If/Ternary)

React conditional rendering explained simply: learn if/else, the ternary operator, and && with beginner-friendly, copy-pasteable code examples.

F
Fepiq Team
Fepiq

"React conditional rendering" simply means showing different things on the screen depending on whether something is true or false. For example: show a "Welcome back" message if a user is logged in, or show a "Please log in" message if they are not. In plain words: it is just an if/else statement, but for the pieces of your app that appear on the page. This guide shows you every common way to do it, with small examples you can copy and run.

What is conditional rendering in React?

In a normal web page, the HTML you write is always shown. In React, you write JavaScript that decides what to show. Conditional rendering is the technique of using a condition (something that is true or false) to pick which piece of the page appears. You already do this every day without thinking about it: "if it is raining, take an umbrella; if not, don't." React components do the same thing with buttons, messages, and lists.

If you have not used React's useState hook yet, read our guide on the React useState Hook for Beginners first, since most of the examples below use it to track whether something is true or false.

A quick reminder: plain JavaScript if/else

Before React, let's look at a plain JavaScript if/else statement. Conditional rendering in React builds directly on this idea.

js
let isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back!");
} else {
  console.log("Please log in.");
}

Line by line: line 1 creates a variable called isLoggedIn and sets it to true. Line 3 checks that variable — if it is true, line 4 runs. Line 5 is the "else" case — it only runs when isLoggedIn is false. React conditional rendering uses this exact same logic, just to decide which JSX (the HTML-like code inside a component) to display.

Method 1: if/else before the return statement

The simplest way to conditionally render in React is to write a normal if/else statement above your component's return, and return different JSX from each branch.

jsx
function Greeting({ isLoggedIn }) {
  if (isLoggedIn) {
    return <h2>Welcome back!</h2>;
  } else {
    return <h2>Please log in.</h2>;
  }
}

Line 1 defines a component called Greeting that receives a prop named isLoggedIn (a prop is just a value passed into a component, like a function argument). Line 2 checks if isLoggedIn is true. If it is, line 3 returns a heading that says "Welcome back!". If it is false, line 5 returns a different heading instead. Only one of the two return statements ever runs.

Method 2: the ternary operator (condition ? a : b)

Writing a full if/else works, but it takes several lines. When you want to choose between two small pieces of JSX inside the same return statement, the ternary operator is shorter. It reads as: "condition ? do this if true : do this if false".

jsx
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h2>Welcome back!</h2> : <h2>Please log in.</h2>}
    </div>
  );
}

The curly braces { } tell React "this is JavaScript, not plain text". Inside them, isLoggedIn ? <h2>Welcome back!</h2> : <h2>Please log in.</h2> means: if isLoggedIn is true, show the first heading; otherwise show the second one. This is the most common way to write conditional rendering in React because it fits neatly inside JSX.

Method 3: the && operator for "show or show nothing"

Sometimes you don't need two options — you just want to show something only when a condition is true, and show nothing at all otherwise. For that, use the && (AND) operator.

jsx
function Inbox({ unreadCount }) {
  return (
    <div>
      <h2>Inbox</h2>
      {unreadCount > 0 && <p>You have {unreadCount} new messages.</p>}
    </div>
  );
}

unreadCount > 0 && <p>...</p> means: if unreadCount > 0 is true, React renders the paragraph. If it is false, React renders nothing at all for that line. This works because && only moves on to the second part if the first part is true.

“Watch out: if the value before && is 0 (not true or false, but the number zero), React will actually render the number 0 on the page instead of nothing. To avoid this, compare it first, like unreadCount > 0, instead of writing just unreadCount &&.”
— Common beginner mistake

Method 4: a lookup object for many conditions

If you have more than two or three options (like a status that can be "loading", "success", or "error"), stacking ternaries becomes hard to read. A cleaner approach is a plain JavaScript object that maps each status to its message.

jsx
function StatusMessage({ status }) {
  const messages = {
    loading: "Loading, please wait...",
    success: "Your order was placed!",
    error: "Something went wrong.",
  };

  return <p>{messages[status]}</p>;
}

Lines 2 to 6 create an object called messages where each key (loading, success, error) points to the text to show. Line 8 looks up the right message using messages[status] — if status is "success", this returns "Your order was placed!". This avoids a long chain of if/else or ternary statements and is easy to add new statuses to later.

Putting it together: a login button example

Here is a full, working component that combines the ternary operator with the useState hook to toggle between two views when a button is clicked.

jsx
import { useState } from "react";

function LoginStatus() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);

  return (
    <div>
      {isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
      <button onClick={() => setIsLoggedIn(!isLoggedIn)}>
        {isLoggedIn ? "Log out" : "Log in"}
      </button>
    </div>
  );
}

Line 4 creates a piece of state called isLoggedIn, starting as false, plus a function setIsLoggedIn to change it. Line 8 uses a ternary to show one paragraph or the other, exactly like before. Line 9 adds a button; when clicked, it calls setIsLoggedIn(!isLoggedIn), which flips true to false or false to true. Line 10 uses another ternary so the button's label always matches the current state. Every time state changes, React automatically re-renders the component with the new condition.

Which method should you use?

SituationBest method
Two different, larger blocks of JSXif/else before the return
Two small pieces of JSX inside the same returnTernary operator (? :)
Show something or show nothing&& operator
Three or more possible optionsLookup object

Common mistakes beginners make

  • Using a number like unreadCount as the condition for && without comparing it to 0 first — this can print a stray "0" on the page.
  • Writing deeply nested ternaries (a ? b : c ? d : e) — this becomes hard to read; use if/else or a lookup object instead once you have more than two options.
  • Forgetting curly braces { } around JavaScript conditions inside JSX — without them, React treats the condition as plain text.
  • Returning nothing when you meant to return null — a component's if branch must return valid JSX or null, not leave the branch empty.

Frequently asked questions

What is conditional rendering in React?+

It is the practice of showing different JSX (React's HTML-like syntax) depending on whether a condition is true or false, such as showing a login form only when a user is not signed in. It uses normal JavaScript tools like if/else, the ternary operator, and &&, not any special React syntax.

What is the difference between if/else and the ternary operator in React?+

if/else must be written as full statements outside the JSX you return, often as separate return statements. The ternary operator (condition ? a : b) is an expression, so it can be written directly inside curly braces in your JSX, which is why it is more common for small conditions.

Can I use a switch statement for conditional rendering?+

Yes, but it needs to go outside the return statement, similar to if/else, because switch is a statement, not an expression. For simple cases with three or more options, a lookup object (a plain JavaScript object mapping each case to its JSX) is usually shorter and easier to read.

Why does React show a 0 on the screen instead of nothing?+

This happens when you write something like count && <p>...</p> and count is the number 0. Since 0 is falsy but is still a value, React renders it as text. Fix it by writing the comparison explicitly, such as count > 0 && <p>...</p>.

How do I show a loading spinner while data is being fetched?+

Store a boolean in state, for example const [isLoading, setIsLoading] = useState(true), set it to false once your data arrives, and use a ternary in your JSX: isLoading ? <Spinner /> : <DataList />. This is one of the most common real-world uses of conditional rendering.

Want to see how conditional rendering works alongside component state in a real hook? Read our beginner guide to the useState Hook.

Read the useState guide

Conditional rendering is one of the most useful skills in React because almost every real app needs to show or hide something: a loading spinner, an error message, a logged-in view, an empty list message. Start with the ternary operator for simple cases, reach for if/else when the JSX gets larger, and use a lookup object once you have several possible options. Practice by rebuilding the login button example above and changing the messages.

Need a developer to build your React app the right way from day one?

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.