Skip to content
All posts

React Performance Tips

Key techniques to keep your React app fast and responsive.

Two profiler flame graphs: a tall one taking 48 ms before and a short one taking 6 ms after, under the words React performance

Performance is one of those things you don't think about - until users start complaining. A slow React app can kill engagement, increase bounce rates, and make even beautiful UI feel broken. The good news is that most performance issues follow predictable patterns, and fixing them doesn't require a rewrite.

React.memo skips re-renders when props haven't changed

By default, a React component re-renders every time its parent re-renders - even if its own props haven't changed. React.memo wraps a component and skips the re-render if props are shallowly equal.

jsx
const UserCard = React.memo(({ name, avatar }) => { return ( <div> <img src={avatar} alt={name} /> <span>{name}</span> </div> ); });

Use this for components that are expensive to render and receive the same props frequently - like list items, cards, or table rows.

Note: React.memo does a shallow comparison. If you pass objects or arrays as props, make sure their references don't change on every render.

memo is useless if you pass a new function every render

When you pass a function as a prop, it gets recreated on every render - which breaks React.memo because the reference changes even if the logic is identical.

jsx
// ❌ New function reference every render const handleClick = () => { doSomething(id); }; // ✅ Stable reference const handleClick = useCallback(() => { doSomething(id); }, [id]);

Pair useCallback with React.memo for maximum effect.

Try it: click Re-render parent a few times and watch which cards flash.

jsx
import { memo, useCallback, useRef, useState } from 'react'; import './styles.css'; function Card({ title, code, onSelect }) { // Counting renders in a ref is fine for a demo; don't do it in real code. const renders = useRef(0); renders.current += 1; return ( // A new key on every render replays the flash animation. <button className="card" key={renders.current} onClick={onSelect}> <strong>{title}</strong> <code>{code}</code> <span>renders: {renders.current}</span> </button> ); } const MemoCard = memo(Card); export default function App() { const [count, setCount] = useState(0); const select = useCallback(() => {}, []); return ( <main> <button className="primary" onClick={() => setCount((c) => c + 1)}> Re-render parent ({count}) </button> <div className="grid"> <Card title="Plain component" code="<Card onSelect={() => {}} />" onSelect={() => {}} /> <MemoCard title="memo + inline function" code="<MemoCard onSelect={() => {}} />" onSelect={() => {}} /> <MemoCard title="memo + useCallback" code="<MemoCard onSelect={select} />" onSelect={select} /> </div> </main> ); }

useMemo is for expensive calculations, not every value

If you're computing something heavy on every render - filtering a large list, running a complex algorithm, building a derived structure - useMemo lets you cache the result and recompute only when dependencies change.

jsx
const filteredPosts = useMemo(() => { return posts.filter(post => post.title.toLowerCase().includes(query.toLowerCase()) ); }, [posts, query]);

Don't overuse it - useMemo itself has overhead. Only reach for it when profiling shows a real bottleneck.

Load routes and heavy components only when needed

Bundling your entire app into one JS file means users download code for pages they may never visit. Code splitting fixes this.

jsx
import { lazy, Suspense } from 'react'; const Dashboard = lazy(() => import('./pages/Dashboard')); function App() { return ( <Suspense fallback={<div>Loading...</div>}> <Dashboard /> </Suspense> ); }

React Router works great with this pattern - lazy load each route component and let Suspense handle the loading state.

Render only the rows on screen

Rendering 1000 DOM nodes at once is slow. If you have a long list - messages, search results, data tables - only render what's visible in the viewport.

bash
npm install @tanstack/react-virtual
jsx
import { useVirtualizer } from '@tanstack/react-virtual'; const List = ({ items }) => { const parentRef = useRef(null); const virtualizer = useVirtualizer({ count: items.length, getScrollElement: () => parentRef.current, estimateSize: () => 64, }); return ( <div ref={parentRef} style={{ height: '500px', overflow: 'auto' }}> <div style={{ height: virtualizer.getTotalSize() }}> {virtualizer.getVirtualItems().map(virtualRow => ( <div key={virtualRow.index} style={{ position: 'absolute', top: virtualRow.start, height: virtualRow.size, }} > {items[virtualRow.index].name} </div> ))} </div> </div> ); };

Keep state close to where it's used

One of the most overlooked optimizations. If a piece of state is only used by one subtree, keep it there - don't lift it to the top level.

jsx
// ❌ App re-renders every time the input changes function App() { const [query, setQuery] = useState(''); return ( <div> <HeavyComponent /> <SearchInput query={query} onChange={setQuery} /> </div> ); } // ✅ Only SearchInput re-renders function App() { return ( <div> <HeavyComponent /> <SearchWidget /> </div> ); } function SearchWidget() { const [query, setQuery] = useState(''); return <SearchInput query={query} onChange={setQuery} />; }

Measure with the Profiler before optimizing

Before optimizing anything, measure. The React DevTools Profiler shows exactly which components re-rendered, how long they took, and why.

  1. Install React Developer Tools for Chrome
  2. Open DevTools → Profiler tab
  3. Hit Record, interact with your app, stop recording
  4. Inspect the flame graph - look for wide bars and unexpected re-renders

Optimize what the profiler shows, not what you guess.

Match the fix to the problem

  • Re-renders → React.memo + useCallback
  • Heavy calculations → useMemo
  • Large bundles → lazy + Suspense
  • Long lists → virtualization
  • State scope → move state down
  • Finding the problem → Profiler first, optimize second