Back to Snippets

Optimizing React Component Performance with the Use Memo Hook

JavaScript JavaScript / jQuery May 20, 2026

This code snippet demonstrates how to use the React use Memo hook to optimize component performance by memoizing expensive function calls and preventing unnecessary re-renders. It is particularly useful when dealing with complex computations or API calls that can slow down your application. The use Memo hook is used to memoize the result of an expensive function call, and the dependency array is updated to prevent stale values from being returned. By utilizing this hook, developers can significantly improve the performance of their React applications.

Snippet Stats

Lines 24
Characters 636
Read 1 min
javascript • 24 lines
import { useMemo, useState } from 'react';

const ExampleComponent = () => {
    const [count, setCount] = useState(0);
    const expensiveCalculation = (count) => {
        // Simulate an expensive calculation
        for (let i = 0; i < 10000000; i++) {
            // Do something expensive
        }
        return count * 2;
    };

    const memoizedCalculation = useMemo(() => expensiveCalculation(count), [count]);
    
    return (
        <div>
            <p>Count: {count}</p>
            <p>Expensive Calculation: {memoizedCalculation}</p>
            <button onClick={() => setCount(count + 1)}>Increment Count</button>
        </div>
    );
};

export default ExampleComponent;

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This React pattern optimizes performance in functional components by preventing unnecessary re-renders using the useMemo and useCallback hooks. It is ...

React Performance Optimization Using useMemo And useCallback Hooks

JavaScript
This React code snippet utilizes the Context API along with the useMemo hook to optimize performance by sharing data between components efficiently. I...

Optimizing React Performance With Context API And useMemo

JavaScript
This code snippet demonstrates how to optimize React hooks using useCallback and useMemo to prevent unnecessary re-renders and improve performance. It...

React Performance Optimization Using UseCallback And UseMemo Hooks