🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
⚛️ React JS

Conditional Rendering

📚 What is Conditional Rendering? Conditional rendering shows different UI based on a condition - the component decides what to render at runtime. You use familiar JavaScript: if/else, ternary (? :), or logical AND (&&). This is how React makes interfaces respond to state and props.

5 min 10 XP Lesson 6 of 23
Conditional Rendering
🌐

Appy Says…

Show a loading spinner while data is fetching. Show an error message if something failed. Show the content when it's ready. Conditional rendering is how you build UIs that respond intelligently to state.

📖

What is Conditional Rendering?

Conditional rendering means showing different JSX based on state or props. React uses JavaScript's own conditional syntax inside JSX.

  • &&: {isLoading && <Spinner />}
  • Ternary: {isError ? <Error /> : <Content />}
  • if/else before return: full if statement outside JSX
  • Early return: if (isLoading) return <Spinner />;
  • Nullish: {user ?? <LoginPrompt />}
  • Rendering nothing: return null from a component
🎮

Think of it like a Minecraft scoreboard condition

In Minecraft command blocks, you can set commands to run only when a score equals X. React conditional rendering works the same — show this UI component only when a condition is true.

⚙️

How It Works

  • 1. {condition && <Component />} — renders Component only if condition is truthy
  • 2. Gotcha: {0 && <X />} renders the number 0! Use {Boolean(count) && ...} or {count > 0 && ...}
  • 3. Ternary: {a ? <A /> : <B />} — always renders one or the other
  • 4. Early return: cleanest for complex conditions — return early before the main JSX
  • 5. return null renders nothing but still mounts/unmounts the component
🌍

Real-World Examples

  • Loading state: {isLoading ? <Skeleton /> : <Content data={data} />}
  • Auth gate: {user ? <Dashboard /> : <LoginPage />}
  • Error banner: {error && <ErrorBanner message={error} />}
  • Admin-only: {user.role === 'admin' && <AdminPanel />}
💡

Key Facts

  • Returning null from a component renders nothing but still triggers lifecycle/effects
  • Short-circuit && is a JS feature — React doesn't add any magic; false and null are valid children that render nothing
  • Prefer ternary for binary cases (show A or B); use && for optional additions
  • Complex conditionals are cleaner as separate variables or early returns than nested ternaries
⚠️

Watch Out!

{count && <Badge />} renders 0 on screen when count is 0 because 0 is falsy in JS but React renders it as text. Fix: {count > 0 && <Badge />} or {!!count && <Badge />}.

📌

Remember

&& for optional show/hide. Ternary for A or B. Early return for complex conditions. Always check for 0 gotcha with &&.

What You Learned

  • Conditional rendering uses &&, ternary, or early return to show/hide JSX
  • Beware &&with numbers — 0 renders; use > 0 check
  • Unlocks: loading states, error messages, auth gates, feature flags

Key Facts

  • Returning null from a component renders nothing but still triggers lifecycle/effects
  • Short-circuit && is a JS feature — React doesn't add any magic; false and null are valid children that render nothing
  • Prefer ternary for binary cases (show A or B); use && for optional additions
  • Complex conditionals are cleaner as separate variables or early returns than nested ternaries

Real-World Examples

• Loading state: <code>{isLoading ? &lt;Skeleton /&gt; : &lt;Content data={data} /&gt;}</code> • Auth gate: <code>{user ? &lt;Dashboard /&gt; : &lt;LoginPage /&gt;}</code> • Error banner: <code>{error && &lt;ErrorBanner message={error} /&gt;}</code> • Admin-only: <code>{user.role === 'admin' && &lt;AdminPanel /&gt;}</code>

Remember

&& for optional show/hide. Ternary for A or B. Early return for complex conditions. Always check for 0 gotcha with &&.

Quick Quiz

1 / 2

Conditional rendering shows?