In React, components don’t always need to render visible UI. Sometimes you only want a component to run logic (like effects, timers, analytics, logging) without adding anything to the DOM.
The simplest way to do this is to return null from the component. Returning null tells React: “render nothing for this component.”
✨ Pattern: “Component with logic only, no UI”
return null; ➜ component renders nothing.null instead of undefined.Any React component can choose to return null instead of JSX:
function MyComponent() { return null; }
This means:
useEffect and manage state when it is mounted.return null; – nothing is rendered, nothing added to the DOM.return <></>; – empty React Fragment; exists in the virtual DOM but does not create a real DOM element.return <div></div>; – empty <div> is rendered into the DOM.This component only renders a warning message when the show prop is true. Otherwise, it returns null and renders nothing.
// Only render the alert when "show" is true
function AlertMessage({ show }) {
if (!show) {
return null;
}
return <div className="alert alert-warning">⚠️ Warning! Something went wrong.</div>;
}
show is false ➜ no alert is rendered.show is true ➜ a yellow warning box appears.Instead of using ternaries or many conditions inside JSX, you can let the component return null early when it has nothing useful to show.
// Show notification only when a message exists
function Notification({ message }) {
if (!message) {
return null;
}
return (
<div className="alert alert-info">
? {message}
</div>
);
}
When message is empty or undefined, the component simply returns null and nothing is rendered. This keeps the JSX simple and easy to read.
You can hide UI parts dynamically based on flags such as isActive, roles, or permissions.
// Render badge only for active users
function UserBadge({ user }) {
if (!user.isActive) {
return null;
}
return (
<div className="badge bg-success">
Active: {user.name}
</div>
);
}
null, so no badge in the UI.Some components exist only to run side effects such as logging, timers, or event listeners. These are perfect candidates for the return null pattern.
// Logic-only component: logs when active
import { useEffect } from "react";
function Logger({ active }) {
useEffect(() => {
if (active) {
console.log("Logger component active!");
}
}, [active]);
return null;
}
The Logger component never renders UI. It only reacts to changes in the active prop and logs to the console. This pattern is common in analytics, feature flags, keyboard listeners, and timers.
return nulluseEffect, useMemo, etc. still run (while mounted).null, React will mount its UI normally.return null is the simplest and cleanest way to skip rendering.null, not undefined, to avoid warnings and confusion.return null with meaningful component names like AuthGuard or Logger to clarify intent.AlertBox component that appears only when a prop visible is true. Otherwise it should return null.Logger-style component that logs props to the console whenever they change, but never renders any UI.return null for cleaner logic.display: none;) versus a component that returns null.Goal: Learn how and when to use the return null pattern to cleanly control component visibility, improve readability, and optimize rendering performance in your React apps.