React Performance Optimization Basics
Core React performance techniques: React 19 Compiler, React.memo, code splitting with React.lazy, virtualized lists, and avoiding wasted renders.
React Performance Optimization Basics
Optimizing React applications is about eliminating wasted re-renders and reducing initial JavaScript bundle payload sizes.
1. When Does React Re-Render?
A React component re-renders when:
- Its own state changes via a setter function (
setState). - Its parent component re-renders (by default, all children re-render recursively).
- A Context value it consumes changes.
2. Preventing Unnecessary Child Re-renders with React.memo
Wrap expensive components with React.memo so they only re-render if their props shallowly change:
import { memo } from "react";
export const HeavyChart = memo(function HeavyChart({ data }: { data: number[] }) {
console.log("Expensive chart rendering...");
return <svg>{/* Complex SVG path calculations */}</svg>;
});3. Code-Splitting with React.lazy and Suspense
Do not force users on mobile devices to download code for pages or heavy modals they haven't opened yet:
import { lazy, Suspense } from "react";
// Dynamically loaded only when rendered:
const HeavyAdminDashboard = lazy(() => import("./AdminDashboard"));
export function App() {
return (
<Suspense fallback={<div>Loading dashboard bundle...</div>}>
<HeavyAdminDashboard />
</Suspense>
);
}4. The React 19 Compiler
In React 19+, the React Compiler automatically memoizes components, values, and functions at build time, eliminating the manual mental overhead of littered useMemo, useCallback, and React.memo calls across application code.
Related Topics
Data Fetching in Modern React
Data fetching paradigms in React: Server Components async/await, TanStack Query (React Query), SWR, and avoiding useEffect fetch waterfalls.
Common React Mistakes and Anti-Patterns
Avoiding frequent React mistakes: mutating state directly, infinite useEffect loops, missing keys, stale closures, and over-using Context.