How to Fix React Component Not Rendering
In this tutorial, you'll learn about How to Fix React Component Not Rendering. We cover key concepts, practical examples, and best practices.
The Problem
A React component does not appear on the screen despite being included in the JSX, typically due to a missing return statement, incorrect import, or a conditional that evaluates to false.
Quick Fix
Step 1: Check the return statement
A component without an explicit return renders nothing:
function UserProfile({ user }) {
<div>
<h1>{user.name}</h1>
</div>
}
Add return:
function UserProfile({ user }) {
return (
<div>
<h1>{user.name}</h1>
</div>
);
}
Step 2: Verify the import path
A component with a wrong import silently renders nothing:
import UserProfile from './components/UserProfile';
function App() {
return <UserProfile />;
}
If the file exports as default, check the filename matches:
import UserProfile from './components/UserProfile';
Step 3: Check conditional rendering
A false condition prevents rendering:
function Dashboard({ isAdmin }) {
return (
<div>
<h1>Dashboard</h1>
{isAdmin && <AdminPanel />}
</div>
);
}
If isAdmin is false, AdminPanel does not render. Log it:
function Dashboard({ isAdmin }) {
console.log('isAdmin:', isAdmin);
return (
<div>
<h1>Dashboard</h1>
{isAdmin && <AdminPanel />}
</div>
);
}
Step 4: Check for naming collisions
A lowercase component name is treated as an HTML tag:
function userProfile() {
return <div>Profile</div>;
}
function App() {
return <userProfile />;
}
React renders an HTML <userprofile> element, not the component. Capitalize:
function UserProfile() {
return <div>Profile</div>;
}
function App() {
return <UserProfile />;
}
Step 5: Check for empty fragments
A component returning an array must have keys:
function Items() {
return [
<li>One</li>,
<li>Two</li>
];
}
Wrap in a fragment:
function Items() {
return (
<>
<li>One</li>
<li>Two</li>
</>
);
}
Prevention
- Use ESLint
react-hooks/rules-of-hooksandreact/jsx-no-undef. - Capitalize all component names.
- Always include an explicit return in component bodies.
- Use TypeScript to catch missing imports and incorrect prop types.
- Add
console.loginside the component to verify it renders.
Common Mistakes with component not rendering
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro