Lesson 10 of 10
Practice & Quiz - React Performance
Test your understanding of React.memo, useMemo, useCallback, code splitting with lazy/Suspense, and list virtualization with a practical exercise and 5 quiz questions.
Practical Exercise - Virtualized List with Memoization
import { useState, useMemo, useCallback, memo, lazy, Suspense } from 'react';
import { FixedSizeList as List } from 'react-window';
// ─── Expensive pure computation ────────────────────────────────
function generateItems(count: number) {
return Array.from({ length: count }, (_, i) => ({
id: i,
name: `Item ${i + 1}`,
value: Math.random() * 1000,
}));
}
// ─── Memoized Row - only re-renders when data[index] changes ──
interface RowProps { index: number; style: React.CSSProperties; data: ReturnType<typeof generateItems>; }
const Row = memo(({ index, style, data }: RowProps) => (
<div style={{ ...style, padding: '0 16px', display: 'flex', justifyContent: 'space-between' }}>
<span>{data[index].name}</span>
<span>${data[index].value.toFixed(2)}</span>
</div>
));
// ─── Code-split heavy chart component ─────────────────────────
const HeavyChart = lazy(() => import('./HeavyChart')); // loaded only when needed
// ─── Main component ────────────────────────────────────────────
export default function VirtualizedDashboard() {
const [filter, setFilter] = useState('');
const [showChart, setShowChart] = useState(false);
// useMemo - recomputes only when filter changes, not on every render
const items = useMemo(() => {
const all = generateItems(10_000);
return filter
? all.filter((i) => i.name.toLowerCase().includes(filter.toLowerCase()))
: all;
}, [filter]);
// useCallback - stable ref so Row's memo isn't invalidated
const handleFilterChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => setFilter(e.target.value),
[]
);
return (
<div>
<input
placeholder="Filter items..."
value={filter}
onChange={handleFilterChange}
/>
<p>{items.length} items</p>
{/* react-window: renders only ~10 DOM nodes regardless of list size */}
<List
height={400}
itemCount={items.length}
itemSize={40}
itemData={items}
width="100%"
>
{Row}
</List>
<button onClick={() => setShowChart(true)}>Load Chart</button>
{showChart && (
// Suspense - shows fallback while HeavyChart bundle downloads
<Suspense fallback={<p>Loading chart...</p>}>
<HeavyChart data={items.slice(0, 20)} />
</Suspense>
)}
</div>
);
}
// npm install react-window
// npm install -D @types/react-windowModule Quiz
React.memo prevents a component from re-rendering when:
Tip
Tip
Practice Practice Quiz React Performance in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Modern React: Use Hooks instead of class lifecycle methods
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz React Performance from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz React Performance is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?