Skip to content
tezvyn:

Describe two patterns for conditional rendering in JSX

Source: react.devMediumHow cards are made

Describe two patterns for conditional rendering in JSX
Summary

Whether you know React conditional rendering.

Key points

First, early return with if/else for branches; second, ternary or logical AND inside JSX for inline toggles.

Watch out for

Reaching for useState or useEffect when simple JS suffices.

What's really being asked

The interviewer wants to see that you understand React renders are driven by JavaScript control flow, not template syntax. They are checking whether you can distinguish between patterns that improve readability and those that create maintenance debt. Specifically, they care if you know when to extract logic out of JSX versus keeping it inline.

The full answer

A strong answer presents two distinct patterns and explains when each wins. First, the early return pattern: inside the component body, use a standard if statement to return the Welcome component when isLoggedIn is true, otherwise return the LoginButton. This works best when the two branches are large, structurally different, or when you want to avoid nesting. Second, the inline expression pattern: inside the parent JSX, use a ternary operator like isLoggedIn ? <Welcome /> : <LoginButton />, or use logical AND like isLoggedIn && <Welcome /> for conditional inclusion without an else. The candidate should note that ternaries are preferred when both branches matter, while logical AND is concise but risky with falsy values like zero. Mentioning that these are ordinary JavaScript expressions inside curly braces shows you understand JSX is syntactic sugar.

The mistakes people make

A red flag is suggesting useState or useEffect to toggle which component shows. That adds unnecessary state and lifecycle complexity when simple props and expressions suffice. Another mistake is nesting multiple ternaries deeply inside JSX, which hurts readability. Returning undefined instead of null from a component can also cause subtle bugs in older React versions, so explicitly returning null is safer when rendering nothing.

What usually comes next

The interviewer might ask how you would handle three or more mutually exclusive states, which pushes you toward an if/else chain or a switch statement rather than nested ternaries. They might also ask about short-circuit pitfalls, such as count && <List /> rendering zero when count is 0, or they might ask how to keep conditional logic testable by extracting it into small helper components.

A concrete example

Suppose a Dashboard component receives a user prop. Pattern one would be: if not user, return LoginButton; otherwise return Welcome with the user name. Pattern two would be inside the return statement: user ? Welcome with the user name : LoginButton. If you only wanted to show a notification banner when messages exist, you would write messages length greater than zero AND Banner with count to avoid the zero-render bug.

Interview question

In React, when is an early return with if/else preferable to an inline ternary inside JSX?

  • a.When branches are large, structurally different, or to reduce nestingCorrect
  • b.When both branches are short UI elements inside the parent return statement
  • c.When conditionally including a single element without an else branch
  • d.When preventing unnecessary re-renders requires stopping execution before hooks
Why?

Early returns improve readability when branches are large, structurally different, or deeply nested. Option B actually describes when an inline ternary is the better choice, not an early return.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles