React throws this error when a component's render() method or function component returns undefined instead of valid JSX, null, or other renderable values. This typically happens due to missing return statements or incorrect arrow function syntax.
This error occurs when React tries to render a component but receives `undefined` from the component's render function. React expects components to return one of several valid values: JSX elements, strings, numbers, arrays, fragments, portals, booleans, null, or undefined (in React 18+). The error most commonly appears in class components when the `render()` method doesn't explicitly return a value, or in function components when using arrow function syntax with curly braces but forgetting the return statement. React distinguishes between intentionally rendering nothing (returning `null`) and accidentally returning nothing (returning `undefined`). In React versions prior to 18, returning `undefined` always threw this error. React 18 made this slightly more lenient, but the error still appears in most cases where a return value is genuinely missing.
For class components, ensure the render() method explicitly returns JSX or null:
// ❌ WRONG - No return statement
class MyComponent extends React.Component {
render() {
<div>Hello World</div>
}
}
// ✅ CORRECT - Explicit return
class MyComponent extends React.Component {
render() {
return <div>Hello World</div>;
}
}
// ✅ CORRECT - Return null if intentionally rendering nothing
class MyComponent extends React.Component {
render() {
if (!this.props.show) {
return null;
}
return <div>Hello World</div>;
}
}Function components using arrow functions must either use implicit return (parentheses) or explicit return (curly braces with return statement):
// ❌ WRONG - Curly braces without return
const MyComponent = () => {
<div>Hello World</div>
};
// ✅ CORRECT - Implicit return with parentheses
const MyComponent = () => (
<div>Hello World</div>
);
// ✅ CORRECT - Explicit return with curly braces
const MyComponent = () => {
return <div>Hello World</div>;
};
// ✅ CORRECT - Single line implicit return
const MyComponent = () => <div>Hello World</div>;Ensure opening parentheses or JSX is on the same line as the return statement to avoid automatic semicolon insertion:
// ❌ WRONG - JavaScript inserts semicolon after return
function MyComponent() {
return
<div>Hello World</div>;
}
// This becomes: return; <div>Hello World</div>;
// ✅ CORRECT - Opening tag on same line
function MyComponent() {
return <div>
Hello World
</div>;
}
// ✅ CORRECT - Opening parenthesis on same line
function MyComponent() {
return (
<div>
Hello World
</div>
);
}Check conditional rendering to make sure every branch returns something:
// ❌ WRONG - Some paths don't return anything
function MyComponent({ isLoggedIn }) {
if (isLoggedIn) {
return <div>Welcome back!</div>;
}
// Missing return for false case
}
// ✅ CORRECT - All paths return
function MyComponent({ isLoggedIn }) {
if (isLoggedIn) {
return <div>Welcome back!</div>;
}
return <div>Please log in</div>;
}
// ✅ CORRECT - Ternary operator ensures return
function MyComponent({ isLoggedIn }) {
return isLoggedIn ? (
<div>Welcome back!</div>
) : (
<div>Please log in</div>
);
}Ensure you're not trying to use statements (like variable declarations) as the return value:
// ❌ WRONG - Variable declaration doesn't return anything
const MyComponent = () => {
const element = <div>Hello</div>;
};
// ✅ CORRECT - Return the variable
const MyComponent = () => {
const element = <div>Hello</div>;
return element;
};
// ✅ CORRECT - Return JSX directly
const MyComponent = () => {
return <div>Hello</div>;
};TypeScript Integration: TypeScript can catch many of these issues at compile time. Enable strict mode and the noImplicitReturns compiler option to get warnings when not all code paths return a value. Type your function components with React.FC or explicit return types to ensure proper return values.
React 18 Changes: React 18 allows components to return undefined in some contexts, making the framework more lenient. However, relying on this behavior is discouraged as it makes intent unclear. Always explicitly return null if you intend to render nothing.
ESLint Rules: Use eslint-plugin-react with the react/require-render-return rule enabled to catch missing return statements in class component render methods during development.
Performance Consideration: Returning null is more performant than returning an empty fragment <></> or empty div when you don't want to render anything, as React can skip reconciliation entirely.
Common Pitfall with Array Methods: When using .map() or .filter() to render lists, make sure you're returning JSX from the callback:
// ❌ WRONG - Curly braces without return
const List = ({ items }) => (
<ul>
{items.map(item => {
<li key={item.id}>{item.name}</li>
})}
</ul>
);
// ✅ CORRECT
const List = ({ items }) => (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);Prop spreading could cause security issues
Prop spreading could cause security issues
Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
React Hook useEffect placed inside a condition
Hook can only be called inside the body of a function component
Hook can only be called inside the body of a function component
Rollup failed to resolve import during build
How to fix "Rollup failed to resolve import" in React