Course contentsShow
← React
Lesson 185 of 2,3966. Component BasicsFree lesson

Return null to Render Nothing

Components can conditionally return null to skip rendering without errors.

Return null to Render Nothing

What you'll learn: How to make a component render nothing at all by returning null.

Sometimes you want a component to exist in your code but not produce any visible output. Maybe a warning only shows when there's an error, or a badge only appears when there's new content. React gives you a simple solution: return null from your component.

The Core Idea

When a function component returns null, React understands this as "don't render anything here." No DOM elements get created, no placeholder appears—it's as if the component wasn't there at all (visually speaking). The component still executes, but produces zero output.

function Warning({ hasError }) {
  if (!hasError) {
    return null;
  }
  
  return <div>Warning: Something went wrong!</div>;
}

Here, Warning only renders a <div> when hasError is true. Otherwise, it returns null and vanishes from the page.

Why This Matters

This pattern is cleaner than wrapping your entire component in a conditional. You can guard at the top with an early return, then write the rest of your JSX assuming the condition is met:

function Badge({ count }) {
  if (count === 0) {
    return null; // Nothing to show
  }
  
  return <span className="badge">{count}</span>;
}

No wrapper needed. No awkward && operators. Just return null and move on.

Important: No Errors

Returning null is completely safe—React expects it. Unlike returning undefined or forgetting a return statement (which causes errors), null is an intentional "render nothing" signal.

Key Takeaway: Return null from a component to skip rendering without errors—it's the cleanest way to conditionally hide a component entirely.