Most React tutorials focus on building apps. Very few teach you how to fix performance issues which is exactly what companies care about.
π¨βπ» Introduction
With around 3 years of experience building frontend applications using React, Iβve worked on developing scalable, user-friendly interfaces and solving real-world performance challenges.
In many projects, I noticed a common patternβapplications worked fine initially but started slowing down as features grew. This led me to explore performance optimization techniques and better state management approaches.
In this article, Iβll share how I improved a React applicationβs performance using techniques like memoization, code splitting, and modern server-state management with tools like TanStack Query.
The goal is simple: not just to build applications that work, but to build applications that are fast, efficient, and production-ready.
π’ The Problem
The app had:
- Slow initial load
- Unnecessary re-renders
- Large bundle size
- Poor user experience on low-end devices
β Step 1: Identifying the Bottleneck
Before optimizing, I used:
- React DevTools Profiler
- Chrome Performance tab
I discovered:
- Components re-rendering unnecessarily
- Large third-party libraries bloating bundle size
β Step 2: Prevent Unnecessary Re-renders
β Before
function UserList({ users }) {
return users.map(user => <UserCard user={user} />);
}
Every parent render caused all UserCard components to re-render.
β
After (Using memo)
function UserList({ users }) {
return users.map(user => <UserCard user={user} />);
}
import React, { memo } from "react";
const UserCard = memo(({ user }) => {
return <div>{user.name}</div>;
});
π Result: Reduced unnecessary renders significantly.
β Step 3: Use useCallback & useMemo Wisely
β Problem
Functions recreated on every render:
const handleClick = () => {
console.log("Clicked");
};
β
Fix
import { useCallback } from "react";
const handleClick = useCallback(() => {
console.log("Clicked");
}, []);
β Step 4: Code Splitting (Huge Impact)
Instead of loading everything upfront:
import Dashboard from "./Dashboard";
β
Use Lazy Loading
import { lazy, Suspense } from "react";
const Dashboard = lazy(() => import("./Dashboard"));
<Suspense fallback={<p>Loading...</p>}>
<Dashboard />
</Suspense>
π Result: Reduced initial bundle size drastically.
β Step 5: Remove Unused Dependencies
I found heavy libraries that were barely used.
β Replaced large libraries with lighter alternatives
β Removed dead code
β Step 6: API Optimization
- Implemented caching
- Reduced redundant API calls
- Used pagination instead of loading everything
β Step 7: Smarter Data Fetching with React Query (TanStack Query)
Managing API state manually with useEffect quickly becomes messy:
- Re-fetching logic
- Caching
- Loading states
- Error handling
Instead of reinventing the wheel, I used TanStack Query.
β Before (Manual Fetching)
useEffect(() => {
fetch("/api/users")
.then(res => res.json())
.then(setData);
}, []);
β
After (React Query)
import { useQuery } from "@tanstack/react-query";
function Users() {
const { data, isLoading, error } = useQuery({
queryKey: ["users"],
queryFn: () =>
fetch("/api/users").then(res => res.json()),
});
if (isLoading) return <p>Loading...</p>;
if (error) return <p>Error...</p>;
return data.map(user => <div key={user.id}>{user.name}</div>);
}
Why This Is Better ???
- β Automatic caching
- β Background refetching
- β Deduplication of requests
- β Built-in loading & error states
π Result: Cleaner code + better performance without extra effort.
π Final Results
- β‘ Faster initial load time
- π Fewer re-renders
- π Smaller bundle size
- π± Better performance on mobile
π― Key Learnings
- Optimization starts with measurement, not guessing
- Small improvements compound into big gains
- Clean architecture = better performance
π‘ Pro Tip
Donβt overuse optimization hooks (useMemo, useCallback). Use them only when needed.
π Conclusion
Building a React app is one thing. Making it fast is what separates good developers from great ones.
If you can talk about performance with real examples, youβll stand out instantly in interviews.
Top comments (0)