How to fix Cannot render null or undefined directly in React in React

ReactINTERMEDIATEMEDIUMUpdated Dec 10, 2025· 109 views

This error occurs when a React component returns undefined instead of null or a valid JSX element. React requires explicit null returns or valid elements. React 18+ allows undefined, but older versions throw an error.

Quick fix

Fixes Cannot render null or undefined directly in React

// Wrong
function MyComponent({ shouldRender }) {
  if (shouldRender) {
    return <div>Content</div>;
  }
  // Missing return or returns undefined
}

// Correct
function MyComponent({ shouldRender }) {
  if (shouldRender) {
    return <div>Content</div>;
  }
  return null; // Explicitly return null
}
  1. Return null instead of undefined
  2. Use ternary operators with null fallback
  3. Handle all conditional branches
  4. Use logical AND (&&) operator safely
react — error
// Wrong
function MyComponent({ shouldRender }) {
if (shouldRender) {
return <div>Content</div>;
}
// Missing return or returns undefined
}
// Correct
function MyComponent({ shouldRender }) {
if (shouldRender) {
return <div>Content</div>;
}
return null; // Explicitly return null
}
Cannot render null or undefined directly in React

What this error means

React components must explicitly return null or a valid JSX element when they have nothing to render. Returning undefined (either implicitly by having no return statement, or explicitly) indicates a missing return value. Before React 18, this was not allowed and would throw an error. The error helps catch bugs where you accidentally forgot a return statement in your component logic. When a component function completes without a return statement (which defaults to undefined in JavaScript), React cannot proceed. Components are expected to return null, a JSX element, or a fragment if they render nothing.

Typical symptoms

  • Error message: 'Nothing was returned from render. This usually means a return statement is missing.'
  • Component fails to render on the page
  • Application crashes with a runtime error
  • Conditional rendering logic doesn't handle all code paths

Common causes

  1. Component function has no return statement or returns undefined implicitly
  2. Early return statement is missing in conditional logic
  3. Using if statements without an else clause that returns null
  4. Forgetting to return null in ternary operator condition
  5. JSX conditional that evaluates to undefined instead of null or false
  6. Using React version older than 18 (React 18+ allows undefined)

How to fix "Cannot render null or undefined directly in React"

1Return null instead of undefined

When your component should render nothing, explicitly return null instead of relying on undefined.

jsx
// Wrong
function MyComponent({ shouldRender }) {
  if (shouldRender) {
    return <div>Content</div>;
  }
  // Missing return or returns undefined
}

// Correct
function MyComponent({ shouldRender }) {
  if (shouldRender) {
    return <div>Content</div>;
  }
  return null; // Explicitly return null
}
2Use ternary operators with null fallback

In JSX expressions, use ternary operators with null as the fallback for false conditions.

jsx
// Wrong
function Component({ show }) {
  return (
    <div>
      {show && <p>Only shown when true</p>}
      {show ? <p>Shown</p> : undefined} {/* undefined causes error */}
    </div>
  );
}

// Correct
function Component({ show }) {
  return (
    <div>
      {show && <p>Only shown when true</p>}
      {show ? <p>Shown</p> : null}
    </div>
  );
}
3Handle all conditional branches

Ensure every code path in your component returns something valid. Use if/else blocks to cover all cases.

jsx
// Wrong - some paths return nothing
function UserCard({ user }) {
  if (!user) {
    return null;
  }
  if (user.banned) {
    return <div>User is banned</div>;
  }
  // Missing else: might return undefined
}

// Correct - all paths have returns
function UserCard({ user }) {
  if (!user) {
    return null;
  }
  if (user.banned) {
    return <div>User is banned</div>;
  }
  return <div>Welcome, {user.name}</div>;
}
4Use logical AND (&&) operator safely

The && operator in JSX works well for simple conditionals. It will render nothing if the condition is false (which React treats like null).

jsx
// Safe - && operator handles false correctly
function Alert({ message, type }) {
  return (
    <div>
      {type === 'error' && <p className='error'>{message}</p>}
      {/* If condition is false, nothing renders */}
    </div>
  );
}
5Check your React version

If you're using React 17 or older, upgrade to React 18+ where undefined rendering is allowed. This eliminates this class of error entirely. Check your package.json:

json
{
  "dependencies": {
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}

If upgrading is not possible, always return null explicitly.

Advanced notes

React 18 introduced a significant change: components can now return undefined without throwing an error. Before React 18, the render method had to return a ReactNode (which excludes undefined). However, for compatibility with older versions and for code clarity, returning null is still the recommended practice. The null return communicates intent—'this component should render nothing'—while undefined can occur accidentally. If you're refactoring legacy code or using TypeScript with strict typing, returning null makes the intent explicit and catches more potential bugs.

Frequently asked questions

What does "Cannot render null or undefined directly in React" mean?
React components must explicitly return null or a valid JSX element when they have nothing to render. Returning undefined (either implicitly by having no return statement, or explicitly) indicates a missing return value. Before React 18, this was not allowed and would throw an error.
How do I fix "Cannot render null or undefined directly in React"?
This error occurs when a React component returns undefined instead of null or a valid JSX element. React requires explicit null returns or valid elements. React 18+ allows undefined, but older versions throw an error.
What causes "Cannot render null or undefined directly in React"?
Component function has no return statement or returns undefined implicitly Early return statement is missing in conditional logic Using if statements without an else clause that returns null
Is the "Cannot render null or undefined directly in React" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading