What is conditional rendering in React?

BeginnerReact interview questionBy , updated 17 November 2026

Short answer

Conditional rendering means a component returns different JSX depending on its props or state, using ordinary JavaScript. You can return early with if, choose between two elements with a ternary, show or hide one element with &&, or return null to render nothing. Watch for {count && <List />}, which renders 0 when the count is zero, and remember that removing a component destroys its state.

if and early return

When whole outputs differ, use if before the return. It reads clearly and handles any number of cases.

function OrderStatus({ order, error }) {
  if (error) return <p role="alert">Could not load the order.</p>;
  if (!order) return <Spinner />;
  if (order.items.length === 0) return <p>Your order is empty.</p>;

  return <OrderDetails order={order} />;
}

Early returns are the cleanest way to handle the loading, error and empty states of data, one at a time, before the main output. One rule applies: every hook must be called before the first early return. A hook below an if (...) return runs on some renders and not others, which breaks the rules of hooks.

Ternary: one thing or another

Inside JSX you can only use expressions, so if is not allowed there. A ternary picks between two branches inline.

<button onClick={toggle}>
  {isPlaying ? <PauseIcon /> : <PlayIcon />}
  {isPlaying ? "Pause" : "Play"}
</button>

Ternaries are fine for two short branches. Nested ternaries quickly become hard to read; move that logic into a variable or a helper component.

&&: show or nothing

condition && <Element /> renders the element when the condition is truthy. When it is falsy, the expression evaluates to the condition itself, and React renders nothing for false, null and undefined.

{isAdmin && <AdminPanel />}
{unreadCount > 0 && <Badge count={unreadCount} />}

The 0 pitfall

React renders numbers, including 0. So this shows a stray "0" on screen when the list is empty:

{messages.length && <MessageList messages={messages} />} // renders 0

0 && anything evaluates to 0, and 0 is rendered as text. NaN shows up the same way. Make the left side a real boolean:

{messages.length > 0 && <MessageList messages={messages} />}

Returning null

A component can return null to render nothing. The component still exists in the tree, its hooks still run, but nothing reaches the DOM.

function Banner({ message }) {
  if (!message) return null;
  return <div className="banner">{message}</div>;
}

Deciding in the parent ({message && <Banner message={message} />}) and deciding in the child (return null) look the same on screen. The difference is that in the second case the child stays mounted and keeps its state.

Many cases: switch or an object map

For three or more cases keyed on one value, a switch in a helper function or an object lookup is clearer than chained ternaries.

const panels = {
  profile: ProfilePanel,
  billing: BillingPanel,
  security: SecurityPanel,
};

function Settings({ tab }) {
  const Panel = panels[tab] ?? NotFoundPanel;
  return <Panel />;
}

The variable must start with a capital letter (Panel) so JSX treats it as a component.

Which to use

Situation Pattern
Loading, error, empty and loaded states Early returns
Two alternatives inline Ternary
Show something or nothing && with a boolean on the left
The component decides it has nothing to show return null
Several cases keyed on one value Object map or switch

Conditional rendering and state

React keeps a component's state as long as the same component type stays at the same position in the tree. Conditional rendering can break that in two ways.

Hiding a component destroys its state. When {showForm && <Form />} becomes false, Form unmounts and its state is gone. Showing it again starts from scratch. To keep the state, either lift it to the parent, hide the element with CSS (hidden or display: none) while it stays mounted, or use React's <Activity mode="hidden">, added in React 19.2, which keeps hidden state and pauses effects.

Swapping props in the same position keeps state. This often surprises people:

{isPlayerA ? <Counter name="Ana" /> : <Counter name="Ben" />}

Both branches render a Counter in the same place, so React treats them as the same component and Ben's counter starts with Ana's score. Give each branch a key (key="ana" and key="ben") to make React reset the state when you switch. Why do lists need keys in React? and What is reconciliation in React? explain the identity rules behind this.

Practise it

Common follow-up questions

Can I use if inside JSX?

No, JSX braces accept expressions only. Use a ternary or && inline, or compute a variable with if above the return.

What is the difference between returning null and undefined?

Since React 18, both render nothing. null is the clearer convention and shows the empty case was intended.

Is it better to hide with CSS or to unmount?

Unmount when the content is rarely shown, expensive, or should start fresh. Hide with CSS when switching is frequent and state, such as a scroll position or a half-filled form, must survive. Hidden content still costs memory and still re-renders.

How do I add a class conditionally?

Use a template literal or an array: className={`tab ${active ? "tab-active" : ""}`}. Libraries such as clsx make longer lists of conditional classes tidier.

Mistakes to avoid

  • Using a number on the left of &&, so 0 appears on screen

  • Calling a hook after an early return

  • Nesting ternaries three levels deep

  • Expecting state to survive when a component is conditionally unmounted

  • Swapping between two instances of the same component without a key and getting each other's state

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Build Switch, Case and Default componentsMedium30 min
  2. Handle every data-fetching state: loading, error with retry, empty results, no stale data and abortMedium35 min
  3. Build tabs whose panels load on first view and keep their content: no duplicate loads, retry and hover preloadMedium40 min
  4. Predict controlled and uncontrolled input values after typing, resetting state and changing keyMedium20 min
  5. Build a schema-driven form with conditional fields and pluggable field typesHard60 minFree