React — Performance
React.memo
const ExpensiveComponent = React.memo(function ExpensiveComponent({ data }) {
console.log('Rendering ExpensiveComponent');
return <div>{data}</div>;
});
useMemo
function ExpensiveCalculation({ items }) {
const sortedItems = useMemo(() => {
return items.sort((a, b) => a - b);
}, [items]);
return (
<ul>
{sortedItems.map(item => (
<li key={item}>{item}</li>
))}
</ul>
);
}
useCallback
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
console.log('Clicked');
}, []);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(c => c + 1)}>Increment</button>
<ExpensiveChild onClick={handleClick} />
</div>
);
}
Lazy Loading
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
Performance Examples
// Virtualized list
import { FixedSizeList } from 'react-window';
function VirtualizedList({ items }) {
const Row = ({ index, style }) => (
<div style={style}>
{items[index]}
</div>
);
return (
<FixedSizeList
height={400}
itemCount={items.length}
itemSize={35}
width={300}
>
{Row}
</FixedSizeList>
);
}
// Debounced search
function SearchInput({ onSearch }) {
const [query, setQuery] = useState('');
const debouncedSearch = useCallback(
debounce((value) => {
onSearch(value);
}, 300),
[onSearch]
);
const handleChange = (e) => {
const value = e.target.value;
setQuery(value);
debouncedSearch(value);
};
return (
<input
value={query}
onChange={handleChange}
placeholder="Search..."
/>
);
}
// Code splitting
const Dashboard = React.lazy(() => import('./pages/Dashboard'));
const Settings = React.lazy(() => import('./pages/Settings'));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
Performance Checklist
- Use React.memo for expensive components
- Use useMemo for expensive calculations
- Use useCallback for functions passed as props
- Implement code splitting with React.lazy
- Use virtualization for long lists
- Debounce expensive operations
- Avoid unnecessary re-renders
Mini Practice
Write React code that:
- Uses React.memo
- Implements useMemo
- Uses useCallback
- Adds code splitting
Up Next
Next: Learn about Testing.
Related Topics
Frequently Asked Questions about Performance
What is Performance in React?
Performance is a fundamental concept in React. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Performance?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Performance.
Why is Performance important in React?
Performance is essential for React development. Understanding this concept will help you write better code and solve real-world problems more effectively.