This error occurs when a React component returns an invalid value like undefined, typically due to a missing return statement. In React versions before 18, components that returned undefined or false would throw this error, though React 18+ is more lenient with undefined returns.
This error appears when a React component fails to return valid JSX or null. It most commonly happens when developers forget to add a return statement in their component function, or when they use incorrect syntax with arrow functions. In JavaScript, when a function doesn't explicitly return a value, it returns undefined by default. React interprets this as an error because it expects components to either return JSX elements or explicitly return null when they should render nothing. The error message "Nothing was returned from render" is React's way of catching this common mistake before it causes rendering issues. While React 18 relaxed this restriction to allow undefined returns (treating them the same as null), earlier versions would throw this error. Even in React 18+, returning undefined is considered poor practice because it's ambiguous whether you intended to return nothing or simply forgot the return statement. Using null is clearer and more intentional.
If your component uses curly braces, ensure you have an explicit return statement:
// ❌ Incorrect - missing return
const MyComponent = () => {
<div>Hello World</div>
};
// ✅ Correct - explicit return
const MyComponent = () => {
return <div>Hello World</div>;
};This is the most common cause of the error. When using curly braces with arrow functions, JavaScript requires an explicit return statement.
Replace curly braces with parentheses for implicit returns:
// ❌ Incorrect - curly braces without return
const MyComponent = () => {
<div>Hello World</div>
};
// ✅ Correct - implicit return with parentheses
const MyComponent = () => (
<div>Hello World</div>
);
// ✅ Also correct - single line implicit return
const MyComponent = () => <div>Hello World</div>;Implicit returns work when you don't need to declare variables or execute logic before returning JSX.
Ensure the opening parenthesis is on the same line as the return keyword:
// ❌ Incorrect - automatic semicolon insertion
function MyComponent() {
return
<div>Hello World</div>
}
// ✅ Correct - opening parenthesis on same line
function MyComponent() {
return (
<div>Hello World</div>
);
}JavaScript's automatic semicolon insertion treats the first example as return; followed by unreachable code, causing the component to return undefined.
When a component should conditionally render nothing, return null instead of undefined or false:
// ❌ Incorrect - returning false
const MyComponent = ({ show }) => {
if (!show) return false;
return <div>Content</div>;
};
// ❌ Incorrect - implicit undefined
const MyComponent = ({ show }) => {
if (!show) return;
return <div>Content</div>;
};
// ✅ Correct - explicit null
const MyComponent = ({ show }) => {
if (!show) return null;
return <div>Content</div>;
};Returning null is the idiomatic way to tell React "render nothing" and makes your intent clear.
Check that every code path in your component returns valid JSX or null:
// ❌ Incorrect - missing return in else branch
const MyComponent = ({ type }) => {
if (type === 'A') {
return <div>Type A</div>;
} else {
<div>Other Type</div>; // Missing return!
}
};
// ✅ Correct - all branches return
const MyComponent = ({ type }) => {
if (type === 'A') {
return <div>Type A</div>;
} else {
return <div>Other Type</div>;
}
};
// ✅ Even better - single return point
const MyComponent = ({ type }) => {
const content = type === 'A' ? 'Type A' : 'Other Type';
return <div>{content}</div>;
};Use your IDE's flow analysis or TypeScript to catch paths that don't return values.
When using .map() or other array methods in JSX, ensure they return elements:
// ❌ Incorrect - missing return in map
const MyComponent = ({ items }) => {
return (
<ul>
{items.map(item => {
<li key={item.id}>{item.name}</li> // Missing return!
})}
</ul>
);
};
// ✅ Correct - explicit return
const MyComponent = ({ items }) => {
return (
<ul>
{items.map(item => {
return <li key={item.id}>{item.name}</li>;
})}
</ul>
);
};
// ✅ Best - implicit return
const MyComponent = ({ items }) => {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
};Using parentheses with arrow functions in .map() enables implicit returns and prevents this error.
React 18 Behavior Change
React 18 relaxed the restriction on undefined returns. Components that return undefined will render nothing instead of throwing an error, matching the behavior of returning null. However, this change was primarily made to support async components and Suspense, not to encourage returning undefined from regular components.
Why Return null Instead of undefined
Even though React 18+ allows undefined, returning null is still recommended for clarity:
- It explicitly communicates intent to render nothing
- It's distinguishable from accidentally forgetting a return statement
- Linters can provide better error messages when you forget to return
- It's compatible with all React versions, not just 18+
TypeScript Considerations
When using TypeScript with React, the React.FC type enforces stricter return types:
// TypeScript error with React.FC and undefined return
const MyComponent: React.FC = () => {
// This may cause TypeScript errors depending on @types/react version
return undefined;
};
// Always safe with null
const MyComponent: React.FC = () => {
return null;
};The TypeScript definitions for React components have evolved, and older versions of @types/react don't allow undefined returns even in React 18+.
Linting Rules
ESLint with the React plugin can catch missing returns:
{
"rules": {
"react/require-render-return": "error",
"consistent-return": "error"
}
}These rules help catch the error at development time before it reaches production.
Class Components
In class components, the render() method must return JSX or null. Forgetting to return from render() causes the same error:
class MyComponent extends React.Component {
render() {
// ❌ Missing return
<div>Hello</div>
}
}
class MyComponent extends React.Component {
render() {
// ✅ Correct
return <div>Hello</div>;
}
}Modern React development favors function components, but the same principles apply to class component render methods.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite