This error occurs when a React Router route component fails to return valid JSX. It typically happens when a component returns undefined, null, or is incorrectly referenced in the route configuration.
This error is thrown by React Router when a route's component function executes but doesn't return a valid React element that can be rendered. React Router expects every route component to return proper JSX that React can process and display. In React, components must return one of several valid types: JSX elements, strings, numbers, null, or fragments. When a component returns undefined (often by forgetting a return statement), returns an invalid type, or encounters an error that prevents proper return, React Router cannot render the route and throws this error. This is a runtime error that occurs during route matching and rendering, meaning the route was successfully matched but the component itself failed to provide renderable output.
Verify that your route component has an explicit return statement:
// ❌ WRONG - Missing return
function MyComponent() {
<div>Content</div>
}
// ✅ CORRECT - Has return statement
function MyComponent() {
return <div>Content</div>
}
// ✅ CORRECT - Arrow function with implicit return
const MyComponent = () => (
<div>Content</div>
);Make sure every code path in your component returns a valid element or null:
// ❌ WRONG - Some paths return undefined
function MyComponent({ isLoading, data }) {
if (isLoading) {
return <div>Loading...</div>
}
if (!data) {
// Missing return!
}
return <div>{data}</div>
}
// ✅ CORRECT - All paths return valid elements
function MyComponent({ isLoading, data }) {
if (isLoading) {
return <div>Loading...</div>
}
if (!data) {
return <div>No data available</div>
}
return <div>{data}</div>
}
// ✅ CORRECT - Using null as fallback
function MyComponent({ isLoading, data }) {
if (isLoading) return <div>Loading...</div>
if (!data) return null
return <div>{data}</div>
}In React Router v6, the element prop must receive JSX, not a component reference:
import { Routes, Route } from 'react-router-dom';
import About from './pages/About';
// ❌ WRONG - Passing component reference
<Routes>
<Route path="/about" element={About} />
</Routes>
// ✅ CORRECT - Passing JSX element
<Routes>
<Route path="/about" element={<About />} />
</Routes>
// ✅ CORRECT - With props
<Routes>
<Route path="/profile" element={<Profile userId={123} />} />
</Routes>Check that your component is properly exported and imported:
// Component file: About.tsx
// ❌ WRONG - No export
function About() {
return <div>About Page</div>
}
// ✅ CORRECT - Named export
export function About() {
return <div>About Page</div>
}
// ✅ CORRECT - Default export
export default function About() {
return <div>About Page</div>
}
// Route file
// ❌ WRONG - Incorrect import for named export
import About from './pages/About'; // Expects default export
// ✅ CORRECT - Matching the export style
import { About } from './pages/About'; // For named export
// OR
import About from './pages/About'; // For default exportIf using TypeScript, ensure components return a single element, not an array:
// ❌ WRONG - Returns array (Element[])
function MyComponent() {
return [
<div key="1">First</div>,
<div key="2">Second</div>
]
}
// ✅ CORRECT - Wrap in fragment
function MyComponent() {
return (
<>
<div>First</div>
<div>Second</div>
</>
)
}
// ✅ CORRECT - Single parent element
function MyComponent() {
return (
<div>
<div>First</div>
<div>Second</div>
</div>
)
}If using TypeScript < 5.1, consider updating to TypeScript 5.1+ which better handles JSX return types.
If your component uses async data, ensure loading states are handled:
// ❌ WRONG - No early return during loading
function MyComponent() {
const [data, setData] = useState(null);
useEffect(() => {
fetchData().then(setData);
}, []);
// data is null initially, might cause issues
return <div>{data.value}</div>
}
// ✅ CORRECT - Handle loading state
function MyComponent() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchData()
.then(setData)
.finally(() => setLoading(false));
}, []);
if (loading) return <div>Loading...</div>
if (!data) return <div>No data</div>
return <div>{data.value}</div>
}React Router Version Differences:
In React Router v5 and earlier, routes used the component prop which accepted a component reference directly. React Router v6 changed this to the element prop which requires JSX. When migrating from v5 to v6, this is a common source of this error.
TypeScript Type Checking:
TypeScript versions before 5.1 use JSX.Element as the return type for components, which doesn't accept arrays or undefined. TypeScript 5.1+ introduced JSX.ElementType which is more permissive. If you're stuck on an older TypeScript version, ensure your components explicitly return single elements.
Higher-Order Components (HOCs):
When using HOCs like withRouter, connect (Redux), or custom wrappers, verify that the HOC properly forwards the component's return value. Some HOCs may inadvertently transform or wrap the return value in a way that breaks React Router's expectations.
Error Boundaries:
Consider wrapping route components in error boundaries to catch and handle these errors gracefully rather than crashing the entire application. This is especially useful during development and debugging.
Development vs Production:
This error may manifest differently in development vs production builds. Development builds include more helpful error messages, while production builds may show generic errors. Always test route navigation in development mode first for easier debugging.
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