Conditional Rendering in React

From the React Components & JSX cheat sheet · JSX Fundamentals · verified Jul 2026

Conditional Rendering

Show/hide elements based on conditions

javascript
function Notifications({ user, count }) {
  return (
    <div>
      {/* Ternary operator */}
      {user ? (
        <h1>Welcome, {user.name}!</h1>
      ) : (
        <h1>Please log in</h1>
      )}
      
      {/* Logical && operator */}
      {count > 0 && (
        <p>You have {count} new messages</p>
      )}
      
      {/* Logical || for defaults */}
      <p>Name: {user?.name || 'Guest'}</p>
      
      {/* Prevent rendering with null */}
      {count === 0 ? null : <span>({count})</span>}
    </div>
  )
}
🟢 Essential - && shows element if condition is true
💡 Ternary (? :) for if-else rendering
⚠️ Remember: 0 and empty string render, null/undefined don't
jsxconditionalessential

More React tasks

Back to the full React Components & JSX cheat sheet